[MDEV-30532] Wrong index chosen by the optimizer for ORDER BY Created: 2023-02-01 Updated: 2024-01-25 |
|
| Status: | Confirmed |
| Project: | MariaDB Server |
| Component/s: | Optimizer |
| Affects Version/s: | 10.2.27, 10.4.20, 10.2.43, 10.4.27 |
| Fix Version/s: | 10.4 |
| Type: | Bug | Priority: | Critical |
| Reporter: | Sasha Pachev | Assignee: | Michael Widenius |
| Resolution: | Unresolved | Votes: | 1 |
| Labels: | triage | ||
| Attachments: |
|
||||||||
| Issue Links: |
|
||||||||
| Description |
|
With the optimizer switch: index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on,index_merge_sort_intersection=off,engine_condition_pushdown=off,index_condition_pushdown=on,derived_merge=on,derived_with_keys=on,firstmatch=on,loosescan=on,materialization=on,in_to_exists=on,semijoin=on,partial_match_rowid_merge=on,partial_match_table_scan=on,subquery_cache=on,mrr=off,mrr_cost_based=off,mrr_sort_keys=off,outer_join_with_cache=on,semijoin_with_cache=on,join_cache_incremental=on,join_cache_hashed=on,join_cache_bka=on,optimize_join_buffer_size=on,table_elimination=on,extended_keys=on,exists_to_in=on,orderby_uses_equalities=on,condition_pushdown_for_derived=on,split_materialized=on,condition_pushdown_for_subquery=on,rowid_filter=off,condition_pushdown_from_having=on and optimizer_use_condition_selectivity set to 1, and task_small1 table loaded from the attachment, we get the following:
It should be using task_task_class_created, which is more specific and still can be used for ORDER BY. I have debugged it with optimizer tracing and in gdb, and got to the point that test_if_skip_sort_order() for some reason is getting called with release_task_ref2 key in tab->ref.key. I attempted to recreate the bug artificially with hand-crafted tables and generated data, but was not successful. The case worked properly choosing the more specific key, and test_if_skip_sort_order was getting called with tab->ref.key set to the more specific key, equivalent of task_task_class_created in this case. In this example, the query still runs fast in spite of using the wrong key, but this is a trimmed/obfuscated production data. On the actual production data the choice of the wrong key results in a factor of 100 slower performance. |
| Comments |
| Comment by Sasha Pachev [ 2023-02-01 ] | ||||||||||||||||||||||||||||||||||||||||
|
10.6.10 is not affected. | ||||||||||||||||||||||||||||||||||||||||
| Comment by Sasha Pachev [ 2023-02-01 ] | ||||||||||||||||||||||||||||||||||||||||
|
10.4.27 is affected, as well as 10.2.27 and 10.2.43. | ||||||||||||||||||||||||||||||||||||||||
| Comment by Aleksey Midenkov [ 2023-02-03 ] | ||||||||||||||||||||||||||||||||||||||||
|
bad.test reproduces on 10.4, but not on 10.5 | ||||||||||||||||||||||||||||||||||||||||
| Comment by Aleksey Midenkov [ 2023-02-06 ] | ||||||||||||||||||||||||||||||||||||||||
|
Fixed in 10.5.3 by commit eb483c5181a Updated optimizer costs in multi_range_read_info_const() and sql_select.cc Updated test in bad.test.txz | ||||||||||||||||||||||||||||||||||||||||
| Comment by Yury Chaikou [ 2023-02-07 ] | ||||||||||||||||||||||||||||||||||||||||
|
Aleksey, we are trying to apply the fix to 10.4.20 but some differences are not trivial to resolve. Could you please port it to 10.4.20 as well? | ||||||||||||||||||||||||||||||||||||||||
| Comment by Sasha Pachev [ 2023-02-08 ] | ||||||||||||||||||||||||||||||||||||||||
|
The following "hammer-slammer" patch fixes the problem for this test case:
Is the original code check correct? Having more key parts is not always bad, as in this case. | ||||||||||||||||||||||||||||||||||||||||
| Comment by Aleksey Midenkov [ 2023-02-08 ] | ||||||||||||||||||||||||||||||||||||||||
|
yury.chaikou Do you follow https://mariadbcorp.atlassian.net/browse/SAMU-97 ? | ||||||||||||||||||||||||||||||||||||||||
| Comment by Sasha Pachev [ 2023-02-11 ] | ||||||||||||||||||||||||||||||||||||||||
|
Better simple patch:
it additionally fixes a production data case which we have not yet isolated to a test case we can share which 10.4.20samu-midenok-samu-97 does not (due to the preference for an index with fewer key parts). It seems that if we say - if the range is good and select limit is OK, choose the index regardless of the key parts, our queries are happier. | ||||||||||||||||||||||||||||||||||||||||
| Comment by Sergei Petrunia [ 2023-06-26 ] | ||||||||||||||||||||||||||||||||||||||||
|
Running the test in 11.0 produces
as the report says
and it's using that index. |