I have a table called mytable and I'm trying to optimize a query on it. I generated the index ind5 on it, and it has the nullable int column col_1 in first place. This query has a WHERE IN
clause, which seems to throw the query optimizer completely off. Here's something that works:
explain select status from mytable use index (ind5)
where col_1 = 10 limit 0,10'
+----+-------------+-------------+------+-------------------+-------------------+---------+-------+--------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------------+------+-------------------+-------------------+---------+-------+--------+-------+
| 1 | SIMPLE | mytable | ref | ind5 | ind5 | 5 | const | 718156 | NULL |
+----+-------------+-------------+------+-------------------+-------------------+---------+-------+--------+-------+
So far so good. Now let's try:
explain select status from mytable use index (ind5)
where col_1 in (1, 10) limit 0,10'
+----+-------------+-------------+------+-------------------+------+---------+------+---------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------------+------+-------------------+------+---------+------+---------+-------------+
| 1 | SIMPLE | mytable | ALL | ind5 | NULL | NULL | NULL | 1436312 | Using where |
+----+-------------+-------------+------+-------------------+------+---------+------+---------+-------------+
What changed here? I'm using a WHERE IN
, but that should be equivalent to col_1 = 1 OR col_1 = 10
, so I would still expect MySQL to use the index, which it's clearly not doing. How about if I do:
explain select status from mytable use index (ind5)
where col_1 = 10 or col_1 = 1 limit 0,10'
+----+-------------+-------------+------+-------------------+------+---------+------+---------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------------+------+-------------------+------+---------+------+---------+-------------+
| 1 | SIMPLE | mytable | ALL | ind5 | NULL | NULL | NULL | 1436312 | Using where |
+----+-------------+-------------+------+-------------------+------+---------+------+---------+-------------+
Lost me completely here. What happened? Why is MySQL not using ind5?
This is MySQL 5.6.
ind5
is. Please provideSHOW CREATE TABLE
.