[MDEV-9586] Sub-optimal query plan is picked from range vs another range vs ref Created: 2016-02-18 Updated: 2023-12-22 |
|
| Status: | Open |
| Project: | MariaDB Server |
| Component/s: | Optimizer |
| Fix Version/s: | 11.5 |
| Type: | Task | Priority: | Major |
| Reporter: | Sergei Petrunia | Assignee: | Sergei Petrunia |
| Resolution: | Unresolved | Votes: | 2 |
| Labels: | None | ||
| Attachments: |
|
| Description |
|
And two query plans:
optimizer's choice between Good and Bad query plans depends on the values of const2A and const2B. Note that both query plans use the same index, but the Good plan uses more key |
| Comments |
| Comment by Sergei Petrunia [ 2016-02-18 ] | |||||||||||
== First query plan ==The plan with type=ref, key=key1, key_len=9 is achieved as follows: 1. Range optimizer chooses a range plan over PK: quick range select, key PRIMARY, length: 8 2. Join optimizer chooses ref access key1 3. We end up using ref access over key1. == Second query plan ==The plan with type=range, key=key1, key_len=17 is chosen as 1. Range optimizer choses a range_plan over key1: quick range select, key key1, length: 17 2. Join optimizer chooses ref access key1 3. There is a heuristic: == Conclusions == 1. Range access and ref access cost estimates do not agree with one another. See in the "Second plan". It first choses a range access, then on step #2 2. What to do? I can't fix the cost model. | |||||||||||
| Comment by Sergei Petrunia [ 2016-02-18 ] | |||||||||||
|
The relevant piece of code from make_join_select():
It's a very old code. | |||||||||||
| Comment by Sergei Petrunia [ 2016-02-18 ] | |||||||||||
|
So, the logic in this piece is
and the change I'm suggesting is:
that is, the heuristics should not break just becase there was another index with a cheaper quick select. | |||||||||||
| Comment by Sergei Petrunia [ 2016-02-19 ] | |||||||||||
|
psergey-ref-or-range-issue-fix1.diff is my first attempt to fix this issue before I saw the above code. It tries to do what is basically described but at another phase. | |||||||||||
| Comment by Sergei Petrunia [ 2016-02-19 ] | |||||||||||
|
Discussed with igor. Solutions:
So, I'll now take psergey-ref-or-range-issue-fix1.diff and move the code there from create_ref_for_key to make_join_select. |