[MDEV-17761] Odd optimizer choice with ORDER BY LIMIT and condition selectivity Created: 2018-11-18 Updated: 2023-12-07 |
|
| Status: | Stalled |
| Project: | MariaDB Server |
| Component/s: | Optimizer |
| Affects Version/s: | 10.1, 10.2, 10.3, 10.4, 10.5, 10.6, 10.7, 10.8, 10.9, 10.10 |
| Fix Version/s: | 10.4 |
| Type: | Bug | Priority: | Major |
| Reporter: | Sergei Petrunia | Assignee: | Sergei Petrunia |
| Resolution: | Unresolved | Votes: | 1 |
| Labels: | order-by-optimization | ||
| Issue Links: |
|
||||||||||||||||||||
| Description |
|
The ORDER BY...LIMIT optimizer displays strange effects when one adds
With current default settings and @@optimizer_use_condition_selectivity=1:
Ok, filtered=100%, the optimizer has no clue about selectivity. Now, the ORDER BY ... LIMIT query:
In order to read 600 rows it will use filesort. (if one uses a lower LIMIT value, e.g. "LIMIT 400", the optimizer will use the Now, let's give optimizer a clue about the condition selectivity:
Now, the optimizer knows about condition selectivity:
The query plan for the ORDER BY...LIMIT query becomes:
The odd parts about this are: (Major) Why did the query plan change from using filesort to using an index? |
| Comments |
| Comment by Sergei Petrunia [ 2018-12-22 ] | ||||||||||||
|
An example with the same use_condition_selectivity setting: The optimizer has no clue about condition' selectivity, assumes it to be 100%, and intends to use filesort:
Ok, using an index to find 600 matching rows was not a good plan, we've used full table scan and filesort. Now, change the condition so that the optimizer knows that the condition' selectivity is 0.9.
| ||||||||||||
| Comment by Sergei Petrunia [ 2018-12-22 ] | ||||||||||||
|
The difference comes from here: in best_access_path():
then, tmp=417, and we reach here:
then, in test_if_cheaper_ordering(), here:
index_scan_time=600 for both, read_time=417 or 615, respectively. To sum up:
| ||||||||||||
| Comment by Sergei Petrunia [ 2019-01-23 ] | ||||||||||||
|
Pushed a patch that fixes the second point (into 10.4) | ||||||||||||
| Comment by Julien Fritsch [ 2023-12-05 ] | ||||||||||||
|
Automated message: |