本文摘自PHP中文网,作者黄舟,侵删。
公司服务用的mysql,最近在查询时时间很慢,经常会上10多秒,查看了一下查询的执行计划,发现索引没有生效。存储引擎使用InnoDB。
一开始在主库查询,一直很好奇为什么索引不生效,切换到备库之后,发现备库是有效的。
开始考虑是不是因为索引出问题,后对索引重建,发现效率高了不少。
简单记录一下对比。
1 2 3 4 5 6 7 | mysql> explain select * from runinfo where status in (0, 2, 1, 3, 4, 7, 9, 10);
+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+
| 1 | SIMPLE | runinfo | All | status_2 | NULL | NULL | NULL | 2378055 | Using where |
+
row in set (0.00 sec)
|
上面是主库的执行计划。
对比一下备库的执行计划。
1 2 3 4 5 6 7 | mysql> explain select * from runinfo where status in (0, 2, 1, 3, 4, 7, 9, 10);
+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+
| 1 | SIMPLE | runinfo | range | status_2 | status_2 | 4 | NULL | 116 | Using where |
+
row in set (0.00 sec)
|
可以看出,备库在查询时适应到索引 status_2。
执行如下的命令之后,问题解决。
1 2 3 4 5 6 7 8 | mysql> OPTIMIZE TABLE runinfo;
+
| Table | Op | Msg_type | Msg_text |
+
| schedule.runinfo | optimize | note | Table does not support optimize, doing recreate + analyze instead |
| schedule.runinfo | optimize | status | OK |
+
rows in set (47.13 sec)
|
以上就是MySQL索引不生效的解决办法的详细内容,更多文章请关注木庄网络博客!
相关阅读 >>
如何利用ssh通道访问mysql
mysql 8.0.17安装教程
几种mysql复制表结构、表数据方法
mysql怎么添加复合主键?
mysql示例讲解数据库约束以及表的设计
mysql如何测试数据库是否连接成功
mysql 5.5 range分区增加删除处理实例详解
初始化mysql的数据库失败怎么办
mysql 3306端口不能访问怎么办
root密码忘记该怎么办
更多相关阅读请进入《mysql》频道 >>
机械工业出版社
本书主要讲述了数据模型、基于对象的数据库和XML、数据存储和查询、事务管理、体系结构等方面的内容。
转载请注明出处:木庄网络博客 » MySQL索引不生效的解决办法