ORDER BY导致索引使用不理想
在mysql中经常出现未按照理想情况使用索引的情况,今天记录一种order by语句的使用导致未按预期使用索引的情况。
1. 问题现象
1.1 sql语句:
select distinct p.* from tb_name p where 1=1 and p.createdate >= '2019-10-23' and p.createdate <= '2019-11-20 24:00:00' and p.status = '1' and p.areaname like '%上海%' order by p.paydate desc limit 0 , 15
1.2 执行计划如下:
+----+-------------+-------+------------+-------+-------------------------------------------------------------+--------------------+---------+------+--------+----------+------------------------------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | extra | +----+-------------+-------+------------+-------+-------------------------------------------------------------+--------------------+---------+------+--------+----------+------------------------------------+ | 1 | simple | p | null | range | createdate,idx_status_paydate | idx_status_paydate | 108 | null | 880063 | 0.74 | using index condition; using where | +----+-------------+-------+------------+-------+-------------------------------------------------------------+--------------------+---------+------+--------+----------+------------------------------------+
1.3 表中索引信息如下:
+------------------+------------+-------------------------------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ | table | non_unique | key_name | seq_in_index | column_name | collation | cardinality | sub_part | packed | null | index_type | comment | index_comment | +------------------+------------+-------------------------------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ | tb_name | 0 | primary | 1 | id | a | 1760103 | null | null | | btree | | | | tb_name | 1 | idx_paydate | 1 | paydate | a | 1734626 | null | null | yes | btree | | | | tb_name | 1 | createdate | 1 | createdate | a | 1736316 | null | null | yes | btree | | | | tb_name | 1 | idx_status_paydate | 1 | status | a | 2 | null | null | yes | btree | | | | tb_name | 1 | idx_status_paydate | 2 | paydate | a | 1741214 | null | null | yes | btree | | | +------------------+------------+-------------------------------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 16 rows in set (0.00 sec)
1.4 理想情况
运行此sql耗时约5.7s。从sql及索引情况来看,使用createdate字段的索引应该会更好才对,为验证此情况,使用force index来强制使用createdate索引运行一次查看结果。
sql改为如下:
select distinct p.* from tb_name p force index (createdate) where 1=1 and p.createdate >= '2019-10-23' and p.createdate <= '2019-11-20 24:00:00' and p.status = '1' and p.areaname like '%上海%' order by p.paydate desc limit 0 , 15
修改后执行计划如下:
root@db09:03:13>explain select distinct p.* from tb_namep force index (createdate) -> where 1=1 and p.createdate >= '2019-10-23' and p.createdate <= '2019-11-20 24:00:00' and p.status = '1' and p.areaname like '%上海%' -> order by p.paydate desc limit 0 , 15; +----+-------------+-------+------------+-------+---------------+------------+---------+------+--------+----------+----------------------------------------------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | extra | +----+-------------+-------+------------+-------+---------------+------------+---------+------+--------+----------+----------------------------------------------------+ | 1 | simple | p | null | range | createdate | createdate | 6 | null | 117858 | 1.11 | using index condition; using where; using filesort | +----+-------------+-------+------------+-------+---------------+------------+---------+------+--------+----------+----------------------------------------------------+ 1 row in set, 3 warnings (0.00 sec)
实际运行该sql耗时约为0.15s,相差约50倍的差距。
1.5 简单分析
从执行计划情况对比来看,使用createdate会进行额外的排序(using filesort),这个不难理解。
2 各种不太合理尝试
2.1 强制使用索引
使用force index (createdate)是可以解决的,此方式上面已经测试过了
2.2 忽略不理想的索引
类似于force index,可以使用ignore index ,其实目的也在于使用上createdate 索引,例如:
select distinct p.* from tb_name p ignore index (idx_status_paydate,idx_paydate) where 1=1 and p.createdate >= '2019-10-23' and p.createdate <= '2019-11-20 24:00:00' and p.status = '1' and p.areaname like '%上海%' order by p.paydate desc limit 0 , 15
其效果和force index 一致,运行耗时也在0.15s左右。
2.3 添加组合索引
将paydate 及createdate 添加为组合索引,但是此举不是一个好办法,执行计划也未按理想情况运行。
3. 相对合理的方式
无论使用force index 还是 ignore index都会影响mysql优化器自身的执行情况。例如createdate 如果范围很大,那么其实走paydate 的索引取前15条记录会更快,为了让应用改动最少且不会因为其他条件的变化而导致未能走合理的索引,选择另一种优化方案,将sql改为如下情况:
select distinct p.* from tb_name p where 1=1 and p.createdate >= '2019-10-23' and p.createdate <= '2019-11-20 24:00:00' and p.status = '1' and p.areaname like '%上海%' order by p.paydate desc, createdate limit 0 , 15
此时执行执行计划如下:
+----+-------------+-------+------------+-------+-------------------------------+------------+---------+------+--------+----------+----------------------------------------------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | extra | +----+-------------+-------+------------+-------+-------------------------------+------------+---------+------+--------+----------+----------------------------------------------------+ | 1 | simple | p | null | range | createdate,idx_status_paydate | createdate | 6 | null | 123024 | 5.55 | using index condition; using where; using filesort | +----+-------------+-------+------------+-------+-------------------------------+------------+---------+------+--------+----------+----------------------------------------------------+ 1 row in set, 3 warnings (0.00 sec)
调整createdate 之后,执行执行计划:
root@db 09:51:00>explain -> select distinct p.* from tb_name p ignore index (idx_status_synis_deletestatus) -> where 1=1 and p.createdate >= '2009-10-23' and p.createdate <= '2019-11-20 24:00:00' and p.status = '1' and p.areaname like '%上海%' -> order by p.paydate desc,createdate desc limit 0 , 15; +----+-------------+-------+------------+------+-------------------------------+--------------------+---------+-------+--------+----------+----------------------------------------------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | extra | +----+-------------+-------+------------+------+-------------------------------+--------------------+---------+-------+--------+----------+----------------------------------------------------+ | 1 | simple | p | null | ref | createdate,idx_status_paydate | idx_status_paydate | 108 | const | 880205 | 5.56 | using index condition; using where; using filesort | +----+-------------+-------+------------+------+-------------------------------+--------------------+---------+-------+--------+----------+----------------------------------------------------+ 1 row in set, 3 warnings (0.00 sec)
也按预期的情况正常。由此看来此方式相对之前的方案是最佳的。
上一篇: HTML常用标签一
推荐阅读
-
解决MySQL中IN子查询会导致无法使用索引问题
-
php中使用exec,system等函数调用系统命令的方法(不建议使用,可导致安全问题)
-
网站快照不更新导致网站没收录禁忌使用的做法
-
Mysql数据库中的 Order by 语句的特殊之处(select 中的项目不必出现在order by中)---不建议使用!
-
ORDER BY导致索引使用不理想
-
如何优雅的使用 参数 is null而不导致全表扫描(破坏索引)
-
MySql中由于Collect导致索引无法使用的解决办法
-
使用 new FormData 上传文件导致IE不兼容的问题
-
关于oracle order by索引是否使用的情况实例讲解
-
php中使用exec,system等函数调用系统命令的方法(不建议使用,可导致安全问题)_PHP教程