V
V
VisualIdeas2019-06-01 13:51:44
MySQL
VisualIdeas, 2019-06-01 13:51:44

Why doesn't MySQL use indexes?

There is a table:
5cf2576d510b5230212138.png
Indexes are created for it:
5cf257d4f3db4764190942.png
But when executing the query:

SELECT `place`.`id`, `place`.`root`, `place`.`lft`, `place`.`rgt`, `place`.`level`, `place`.`type`, `place`.`slug`, `place`.`latitude`, `place`.`longitude`, `place`.`time_zone_id`, `place`.`count_views` 
FROM `my_geoservices`.`place` 
WHERE `place`.`rgt` >= 29602 AND `place`.`root` = 2

MySQL refuses to use indexes:
5cf2589bddc5a109679701.png
Moreover, the index is even in the posible case ((((

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
BorLaze, 2019-06-01
@VisualIdeas

A very common misconception is that if there is an index, then it is always used.
Nothing like this. Moreover, even in the same query, but with different data, the index may or may not be taken into account.
For example, we have a million records with a boolean field, and an index on it (nonsense, of course, but just right for an example). And out of this million, five records have true in this field, and the rest have false.
So, select * from table where boolField = true index is enabled, but select * from table where boolField = false is not. Because, from the point of view of the optimizer, it is justified to search first in the index and then in the table for five positions, but it is completely redundant to do almost the same work for 999,995 records.
This is, of course, on the fingers, but the meaning is something like this.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question