我们正在将 mysql 从 5.5 升级到 5.6,现在一些查询非常慢。
以前需要 0.005 秒的查询现在需要 49 秒。
5.6 上的查询正在跳过索引,似乎:
+----+-------------+-------+-------+----------------------------------------------------+---------+---------+------+--------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+-------+----------------------------------------------------+---------+---------+------+--------+-------------+
| 1 | SIMPLE | pens | index | index_contents_on_slug,index_contents_on_slug_hash | PRIMARY | 4 | NULL | 471440 | Using where |
+----+-------------+-------+-------+----------------------------------------------------+---------+---------+------+--------+-------------+
1 row in set (0.00 sec)
但不会在 5.5 上被跳过:
+----+-------------+-------+-------------+----------------------------------------------------+----------------------------------------------------+---------+------+------+----------------------------------------------------------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+-------------+----------------------------------------------------+----------------------------------------------------+---------+------+------+----------------------------------------------------------------------------------------------+
| 1 | SIMPLE | pens | index_merge | index_contents_on_slug,index_contents_on_slug_hash | index_contents_on_slug_hash,index_contents_on_slug | 768,768 | NULL | 2 | Using union(index_contents_on_slug_hash,index_contents_on_slug); Using where; Using filesort |
+----+-------------+-------+-------------+----------------------------------------------------+----------------------------------------------------+---------+------+------+----------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
两个数据库都是从同一个 mysql 转储创建的。
我在 5.6 上做 import 的时候是不是没有构建这些索引?如何强制创建索引?
查询:
SELECT `pens`.* FROM `pens` WHERE (slug_hash = 'style' OR slug = 'style') ORDER BY `pens`.`id` DESC LIMIT 1
编辑:删除架构
最佳答案
最终上面接受的答案是正确的。
@RandomSeed 的帮助让我朝着正确的方向思考。基本上,5.6 中创建的优化计划与 5.5 中的优化计划有很大不同,因此您可能需要像我一样重新处理查询。
我最终没有使用 FORCE INDEX
,而是删除了部分查询,直到我确定导致 5.6 错过索引的原因。然后我重新设计了应用程序逻辑来处理这个问题。
关于mysql - mysql从5.5升级到5.6后查询慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17931246/