Strange index usage in mysql

Explain
SELECT `feed_objects`.*
FROM `feed_objects`
WHERE (`feed_objects`.feed_id IN
  ( 165,160,159,158,157,153,152,151,150,149,148,147,129,128,127,126,125,124,122,
    121, 120,119,118,117,116,115,114,113,111,110)) ;

+----+-------------+--------------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table        | type | possible_keys | key  | key_len | ref  | rows | Extra       |
+----+-------------+--------------+------+---------------+------+---------+------+------+-------------+
|  1 | SIMPLE      | feed_objects | ALL  | by_feed_id    | NULL | NULL    | NULL |  188 | Using where |
+----+-------------+--------------+------+---------------+------+---------+------+------+-------------+

No index used by_feed_id


But when I specify less values ​​in WHERE- everything works correctly

Explain
SELECT `feed_objects`.*
FROM `feed_objects`
WHERE (`feed_objects`.feed_id IN
  (165,160,159,158,157,153,152,151,150,149,148,147,129,128,127,125,124)) ;

+----+-------------+--------------+-------+---------------+------------+---------+------+------+-------------+
| id | select_type | table        | type  | possible_keys | key        | key_len | ref  | rows | Extra       |
+----+-------------+--------------+-------+---------------+------------+---------+------+------+-------------+
|  1 | SIMPLE      | feed_objects | range | by_feed_id    | by_feed_id | 9       | NULL |   18 | Using where |
+----+-------------+--------------+-------+---------------+------------+---------+------+------+-------------+

Index Used by_feed_id


What is the problem?

+3
source share
1 answer

The MySQL optimizer makes many decisions that sometimes seem strange. In this particular case, I believe that you have a very small table (only 188 rows from the appearance of the first EXPLAIN), and this affects the decision of the optimizer.

The " How MySQL Uses Indexes " manual pages offer this piece of information:

MySQL , . , , MySQL . ( , , .)

WHERE , MySQL , , , , , .

, EXPLAIN. - MySQL , .

+6
source

Source: https://habr.com/ru/post/1739820/


All Articles