[MDEV-23042] View execution slower than underlying query Created: 2020-06-29  Updated: 2020-07-01

Status: Open
Project: MariaDB Server
Component/s: Views
Fix Version/s: None

Type: Task Priority: Major
Reporter: Bernd Jänichen Assignee: Unassigned
Resolution: Unresolved Votes: 0
Labels: performance

Attachments: File mapTest.patch     File mariadbViewPerformance.patch     File test.sql    

 Description   

Execution of a query on a view is up to 3 times slower in comparison to the underlying query even if there's no data within underlying tables.

Root cause is function check_duplicate_names() in sql/sql_view.cc which tests for duplicate column-names within views or derived queries.

It is invoked in selects accessing a view though this check has already been made at view-creation time.

However the implementation in check_duplicate_names() can be improved to gain better performance: mariadbViewPerformance.patch
This patch has been tested in 10.1.45 but should be applicable to subsequent releases as well.

Testing of performance improvement can be done with attached SQL file test.sql and mysqlslap:

mysql -h 127.0.0.1 -u root test < test.sql
mysqlslap -h 127.0.0.1 -u root --create-schema=test -c 100 -q "SELECT * FROM views_perf;"



 Comments   
Comment by Vladislav Vaintroub [ 2020-06-29 ]

without looking why VIEWs do the check multiple times, only about the proposed patch - for a short list, the STL map will be slower due to necessary allocations for temporary objects, and O(N^2) search like today would beat O(N*logN) in proposed patch. I'd guess, in most cases those Lists are tiny.

Comment by Bernd Jänichen [ 2020-06-30 ]

Good point, had only huge lists in mind, though for short-lists i guess the difference of using STL maps compared to a nested loop will be in micro-secs.
Anyway will test that and maybe modify proposed patch.

Comment by Bernd Jänichen [ 2020-07-01 ]

So i've done extensive testing and optimizations for the map lookups see attached test-patch.

You're right that for short-lists the performance is lower compared to the original implementation
but the difference is in fact within a few microseconds.
I.e. for 10 elements the difference is just 5µs.

Starting with 70-90 elements performance of the lookup begins to be better than the original implementation.
For 4k elements the nested while-loop needs 78568µs and the map 1389µs which is a significant improvement.

These numbers are not representative as i tested it with a single linux-machine only but it shows that
there is an improvement.

I do understand that for OLTP use-cases a number of 4000 elements/columns per view makes no sense
but for denormalized OLAP/datawarehouse scenarios it is quite common to have hundrets of columns for performance reasons.

To avoid minimal performance loss in OLTP scenarios the map-function will be used only if the threshold of 100 elements is exceeded.

Comment by Vladislav Vaintroub [ 2020-07-01 ]

Yes, hybrid approach (O(N^2) for small list, O(N*logN) for large ones) is probably better that either one

Generated at Thu Feb 08 09:19:24 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.