MySQL索引不生效的解决办法
程序员文章站
2022-03-08 11:08:33
...
公司服务用的mysql,最近在查询时时间很慢,经常会上10多秒,查看了一下查询的执行计划,发现索引没有生效。
存储引擎使用InnoDB。
一开始在主库查询,一直很好奇为什么索引不生效,切换到备库之后,发现备库是有效的。
开始考虑是不是因为索引出问题,后对索引重建,发现效率高了不少。
简单记录一下对比。
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)
上面是主库的执行计划。
对比一下备库的执行计划。
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。
执行如下的命令之后,问题解决。
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索引不生效的解决办法的详细内容,更多请关注其它相关文章!
推荐阅读
-
BarTender 2016数据库连接不上的图文解决办法
-
MySQL一个索引最多有多少个列?真实的测试例子
-
新装MySql后登录出现root帐号提示mysql ERROR 1045 (28000): Access denied for use的解决办法
-
MySQL错误“Specified key was too long; max key length is 1000 bytes”的解决办法
-
微信小程序中hidden不生效原因的解决办法
-
MySQL5.7 group by新特性报错1055的解决办法
-
Can't connect to MySQL server的解决办法
-
Windows 64 位 mysql 5.7以上版本包解压中没有data目录和my-default.ini及服务无法启动的快速解决办法(问题小结)
-
Windows下MySQL服务无法停止和删除的解决办法
-
mysql 存储过程判断重复的不插入数据