Description:
When only a small range scan needs to be done (say 1/6 the table) the entire
table is scanned.
How to repeat:
see attached test.sql file
mysql test < test.sql
select count(*) from idxtest;
+----------+
| count(*) |
+----------+
| 1280 |
+----------+
explain select val2 from idxtest where val1 > 722 and val1 < 1000;
+----+-------------+---------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows |
Extra |
+----+-------------+---------+------+---------------+------+---------+------+------+-------------+
| 1 | SIMPLE | idxtest | ALL | val1 | NULL | NULL | NULL | 1280 |
Using where |
+----+-------------+---------+------+---------------+------+---------+------+------+-------------+
explain select val2 from idxtest where val1 > 723 and val1 < 1000;
+----+-------------+---------+-------+---------------+------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows |
Extra |
+----+-------------+---------+-------+---------------+------+---------+------+------+-------------+
| 1 | SIMPLE | idxtest | range | val1 | val1 | 5 | NULL | 220 |
Using where |
+----+-------------+---------+-------+---------------+------+---------+------+------+-------------+
explain select val2 from idxtest force index (val1) where val1 > 722 and val1 < 1000;
+----+-------------+---------+-------+---------------+------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows |
Extra |
+----+-------------+---------+-------+---------------+------+---------+------+------+-------------+
| 1 | SIMPLE | idxtest | range | val1 | val1 | 5 | NULL | 222 |
Using where |
+----+-------------+---------+-------+---------------+------+---------+------+------+-------------+
explain select val2 from idxtest force index (val1) where val1 > 723 and val1 < 1000;
+----+-------------+---------+-------+---------------+------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows |
Extra |
+----+-------------+---------+-------+---------------+------+---------+------+------+-------------+
| 1 | SIMPLE | idxtest | range | val1 | val1 | 5 | NULL | 220 |
Using where |
+----+-------------+---------+-------+---------------+------+---------+------+------+-------------+
Suggested fix:
I thought we would do a range scan unless 50% of the rows would be scanned?