MySQL LIKE查询将5.1到5.6中的更改更改为全表扫描

时间:2014-07-30 13:16:18

标签: mysql django mysql-5.6 mysql-5.5 mysql-5.1

出于某种原因,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 |
+----+-------------+--------------+-------+---------------+---------+---------+------+-------------+-------------+

2 个答案:

答案 0 :(得分:0)

您在5.1实例上的统计信息可能是陈旧的,并且可能在该表中的行数较少时收集。

建议:在5.6上,设置全局innodb_stats_persistent_sample_pages = 300;分析表;并检查查询执行情况。

答案 1 :(得分:0)

事实证明这是预期的效果。从MySQL 5.5开始numpy.arange不再uses available indexes

我正在制作一个Django查询,该查询使用LIKE BINARY 1进行查询,默认情况下会执行BINARY查询。

我能够通过执行key__startswith来解决此问题,key__istartswith对密钥执行case insensitive query。这对我的用例很好,因为一切都是小写的。