[MDEV-19532] Query with subqueries hangs for long time with default optimizer_search_depth Created: 2019-05-20 Updated: 2021-02-12 Resolved: 2019-05-23 |
|
| Status: | Closed |
| Project: | MariaDB Server |
| Component/s: | Data Manipulation - Subquery, Storage Engine - MyISAM |
| Affects Version/s: | 10.0, 10.1, 10.3.14, 10.3.15, 10.2, 10.3, 10.4 |
| Fix Version/s: | N/A |
| Type: | Bug | Priority: | Major |
| Reporter: | Tom Dolezal | Assignee: | Igor Babaev |
| Resolution: | Not a Bug | Votes: | 0 |
| Labels: | SELECT, optimizer, subquery | ||
| Environment: |
Windows 10 x64, MariaDB 10.3.15 with none my.ini file |
||
| Attachments: |
|
||||||||||||
| Issue Links: |
|
||||||||||||
| Description |
|
We have found a difference in subquery/join optimizer between MySQL 5.x and MariaDB 10.3.x. When we run a simple select query with several subqueries on MariaDB with the default value of configuration "optimizer_search_depth" (62), the query is executing for very long time, hangs in "statistics" state, with quadratic or exponencional growing of the execution time. The same query performs on MySQL 5 during a few milliseconds. We have prepared test suite. The "prepare.sql" prepares test environment a the "test.sql" demonstrates this problem. In test.sql, the first 10 subqueries are uncommented. Query in this form takes arround 6 seconds, but when we add 11. subquery, query takes 112 second already. When we have changed "optimizer_search_depth" to 1, the execution of same query was done immediately. Same result we got on Windows and Linux, in version 10.3.14 and 10.3.15 |
| Comments |
| Comment by Alice Sherepa [ 2019-05-21 ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Thanks a lot! Reproducible on MariaDB 10.0-10.4, also with optimizer_switch='semijoin=on'.
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Igor Babaev [ 2019-05-21 ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Hi Tom, MariaDB 10.0 introduced a new subquery optimization called 'EXISTS-to-IN Optimization' that all MySQL versions still does not have.
The latter is subject to semi-join conversion that transforms it into the sequence of 14 semi-join operations. As the semi-join operation is commutative the optimizer has to evaluate 15! permutations. No wonder it takes a lot of time if you use the default setting for optimizer_search_depth[=62]. If you change the setting to 0 the optimizer will choose a proper number for search depth (now it's 7).
Unfortunately now neither MariaDB nor MySQL cost-base optimizer can take into account the time spent by the optimizer to choose an optimal plan. If you submit the converted query to MySQL optimizer it will 'hang' as well as in MariaDB optimizer. If you are satisfied with my comment please let me know and I'll close the tickets as 'Not a Bug'. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Tom Dolezal [ 2019-05-22 ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Hi Igor, thanks for detailed explanaition, but I don't understand why MariaDB 10 does this "optimization" and converts EXISTS to IN if the result is much more time-cost than older version of optimizer in MariaDB 5 without this optimization. And second, shouldn't be the default value of optimizer_search_depth 0 if the current default value 62 is not optimal now? You right. The settings optimizer_search_depth to 0 si much more better but still little bit slower than older version of MySQL (0,216 sec. vs. 0,000 sec.) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Igor Babaev [ 2019-05-22 ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Tom, 1. Do you understand why MySQL/MariaDB optimizer does semi-join optimizations? | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Tom Dolezal [ 2019-05-22 ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Igor, 1. Yes, you describe it at https://mariadb.com/kb/en/library/semi-join-subquery-optimizations/ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Igor Babaev [ 2019-05-22 ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Tom, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Igor Babaev [ 2019-05-23 ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
See my explanations in the comments |