欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

ORDER BY导致索引使用不理想

程序员文章站 2022-11-07 10:01:49
在MySQL中经常出现未按照理想情况使用索引的情况,今天记录一种Order by语句的使用导致未按预期使用索引的情况。 1. 问题现象 1.1 SQL语句: SELECT DISTINCT p.* FROM tb_name p WHERE 1=1 AND p.createDate >= '2019- ......

在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)

也按预期的情况正常。由此看来此方式相对之前的方案是最佳的。