[MDEV-29121] Optimizer does not use the index for SELECT while it gives notable improvement in the execution time when FORCEd Created: 2022-07-18 Updated: 2023-10-30 |
|
| Status: | Open |
| Project: | MariaDB Server |
| Component/s: | Optimizer |
| Fix Version/s: | None |
| Type: | Task | Priority: | Major |
| Reporter: | Valerii Kravchuk | Assignee: | Unassigned |
| Resolution: | Unresolved | Votes: | 0 |
| Labels: | None | ||
| Attachments: |
|
||||||||
| Issue Links: |
|
||||||||
| Description |
|
For the specific table and data:
the index is not used for this query:
even though WHERE clause is covered by the index (in a way) and the index is clearly beneficial when forced:
and is used by the optimizer for other content of the table:
The statistics for the table in the real case (InnoDB persistent one) is close to reality and was collected over as many sample pages as we have data pages. But looks like number of rows that match the condition on the first column in the index forces optimizer to ignore it. |
| Comments |
| Comment by Sergei Petrunia [ 2022-10-09 ] | ||||||||||||
|
The report says:
but then:
(I've formatted the query in both cases). But it's the same query? Please clarify this. | ||||||||||||
| Comment by Sergei Petrunia [ 2022-10-09 ] | ||||||||||||
|
There is only one index that's usable for the query:
The WHERE condition is:
. range access is only able to use the first restriction:
it matches 806K rows (out of 1.4M rows in the table). | ||||||||||||
| Comment by Sergei Petrunia [ 2022-10-09 ] | ||||||||||||
|
however, as ANALYZE output with FORCE INDEX shows, the whole WHERE is very selective, it matches just 46 rows:
That is, the pushed index condition is very selective. It filters out most of the rows. | ||||||||||||
| Comment by Sergei Petrunia [ 2022-10-09 ] | ||||||||||||
|
One can imagine the optimizer figuring out that ICP condition would be very selective but I don't expect us being able to add this feature soon (and even if we added it, it would require some extra statistics). | ||||||||||||
| Comment by Sergei Petrunia [ 2022-10-09 ] | ||||||||||||
|
Why not just add indexes? If the conditions on mp_transferplate or mp_outputplate are always very selective, use:
Alternatively can add one index
Then, index_merge will be used and I expect the queries to run very fast. | ||||||||||||
| Comment by Valerii Kravchuk [ 2022-10-10 ] | ||||||||||||
|
I've asked customer to provide some outputs to clarify the impact of your new indexes suggested. But I think that adding some statistics to help optimizer decide that ICP in this case would produce a very selective condition is a way to proceed here, no matter what workaround (FORCE or new indexes) may help in this specific case. | ||||||||||||
| Comment by Ralf Gebhardt [ 2022-10-18 ] | ||||||||||||
|
psergei, I am changing this to a tasks for now with a lower priority, given that a solution was provided. Please comment if this is a edge case use case. |