[MDEV-16450] Optimizer seems to make incorrect choice when AND clause on 2 varchar columns and those 2 columns are indexed (includes ORDER BY different_column and LIMIT 1) Created: 2018-06-08 Updated: 2021-04-22 |
|
| Status: | Stalled |
| Project: | MariaDB Server |
| Component/s: | Optimizer |
| Fix Version/s: | None |
| Type: | Task | Priority: | Major |
| Reporter: | Chris Calender (Inactive) | Assignee: | Ralf Gebhardt |
| Resolution: | Unresolved | Votes: | 1 |
| Labels: | order-by-optimization | ||
| Issue Links: |
|
||||||||
| Description |
|
We are seeing where a seemingly simple query is not using the correct index, and instead preferring an full table scan, which is resulting in a slower query. Here is a simplified version of the CREATE TABLE and SELECT:
If I run this through EXPLAIN with 0 data, I see:
If I add some rows, I still see the same:
If I remove the "LIMIT 1", then it chooses the correct index. Note that in the above EXPLAIN, only 1 index is listed for "possible_keys", yet the other is chosen for "key", so this tells me the full table scan was preferred over the index, and then the secondary index was used for the ORDER BY. This incorrect choice leads to slower times as opposed to when it chooses the correct index (or FORCE INDEX is used). |
| Comments |
| Comment by Alice Sherepa [ 2018-06-11 ] | |||||||||||||||||||||||||||||||||||||||||||||||||
|
Reproduced on MariaDB 10.1-10.3
the results differ when InnoDB or MyISAM engine is used, or int instead of varchar datatype.
| |||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Varun Gupta (Inactive) [ 2018-07-18 ] | |||||||||||||||||||||||||||||||||||||||||||||||||
|
If we look closely at both the analyze statements for the different queries , we don't see key k2 as For the first query (with limit = 1) there are 2 plans
Plan 2
There is a cost based approach to decide between these 2 plans, so for the first query we chose plan 1 and for the second query we chose plan 2. | |||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Varun Gupta (Inactive) [ 2018-07-18 ] | |||||||||||||||||||||||||||||||||||||||||||||||||
|
Another idea that psergey came up with we could do both:
This way the worst case performance would be better but this is more of a new optimizer feature than a bugfix. |