出于某种原因,MySQL 5.6 会进行全表扫描,而 MySQL 5.1 则不会。为什么会这样?
编辑:两个表具有相同的确切索引
在 MySQL 5.1 上
mysql> explain SELECT `kv`.`key`, `kv`.`value` FROM `kv` WHERE `kv`.`key` LIKE BINARY 'weit-149411-%';
+----+-------------+--------------+-------+---------------+---------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+--------------+-------+---------------+---------+---------+------+------+-------------+
| 1 | SIMPLE | kv | range | PRIMARY | PRIMARY | 192 | NULL | 182 | Using where |
+----+-------------+--------------+-------+---------------+---------+---------+------+------+-------------+
在 MySQL 5.6 上
mysql> explain SELECT `kv`.`key`, `kv`.`value` FROM `kv` WHERE `kv`.`key` LIKE BINARY 'weit-149411-%';
+----+-------------+--------------+-------+---------------+---------+---------+------+-------------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+--------------+-------+---------------+---------+---------+------+-------------+-------------+
| 1 | SIMPLE | kv | ALL | PRIMARY | NULL | NULL | NULL | 108364642 | Using where |
+----+-------------+--------------+-------+---------------+---------+---------+------+-------------+-------------+