Mysql5.7 order by 影响 产生 ICP

Posted 翔之天空

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Mysql5.7 order by 影响 产生 ICP相关的知识,希望对你有一定的参考价值。

 

 

mysql5.7 再用order by时候 ,有一种情况 会对已用索引的条件进行ICP处理,正常不应该出现,Mysql8.0无此现象产生

 

示例如下:


--orderid有普通索引,ct字段无索引,主键是id
create table table1 (
id int not null ,
orderid varchar(10) not null,
ct int not null,
primary key(id),
key idx_orderid(orderid)
);


insert into table1 values(1,'1111111111',11);
insert into table1 values(2,'1111111112',12);
insert into table1 values(3,'1111111113',13);
insert into table1 values(4,'1111111114',14);
insert into table1 values(5,'1111111115',15);
insert into table1 values(6,'1111111116',16);


--5.7测试

--没有order by就不出现Using index condition; 
mysql> explain select ct from table1  where orderid='1111111113' ;
+----+-------------+--------+------------+------+---------------+-------------+---------+-------+------+----------+-------+
| id | select_type | table  | partitions | type | possible_keys | key         | key_len | ref   | rows | filtered | Extra |
+----+-------------+--------+------------+------+---------------+-------------+---------+-------+------+----------+-------+
|  1 | SIMPLE      | table1 | NULL       | ref  | idx_orderid   | idx_orderid | 32      | const |    1 |   100.00 | NULL  |
+----+-------------+--------+------------+------+---------------+-------------+---------+-------+------+----------+-------+
1 row in set, 1 warning (0.00 sec)




--order by ct时候 会出现:Using index condition; 
mysql> explain select ct from table1  where orderid='1111111113' order by ct;
+----+-------------+--------+------------+------+---------------+-------------+---------+-------+------+----------+---------------------------------------+
| id | select_type | table  | partitions | type | possible_keys | key         | key_len | ref   | rows | filtered | Extra                                 |
+----+-------------+--------+------------+------+---------------+-------------+---------+-------+------+----------+---------------------------------------+
|  1 | SIMPLE      | table1 | NULL       | ref  | idx_orderid   | idx_orderid | 32      | const |    1 |   100.00 | Using index condition; Using filesort |
+----+-------------+--------+------------+------+---------------+-------------+---------+-------+------+----------+---------------------------------------+
1 row in set, 1 warning (0.02 sec)




--8.0测试

--order by ct时候 ,不会出现 Using index condition; 
mysql> explain select ct from table1  where orderid='1111111113' order by ct;
+----+-------------+--------+------------+------+---------------+-------------+---------+-------+------+----------+----------------+
| id | select_type | table  | partitions | type | possible_keys | key         | key_len | ref   | rows | filtered | Extra          |
+----+-------------+--------+------------+------+---------------+-------------+---------+-------+------+----------+----------------+
|  1 | SIMPLE      | table1 | NULL       | ref  | idx_orderid   | idx_orderid | 42      | const |    1 |   100.00 | Using filesort |
+----+-------------+--------+------------+------+---------------+-------------+---------+-------+------+----------+----------------+
1 row in set, 1 warning (0.01 sec)

 

 

下面是5.7和8.0 的 trace 对比和说明:

 

 

 

以上是关于Mysql5.7 order by 影响 产生 ICP的主要内容,如果未能解决你的问题,请参考以下文章

Mysql5.7中子查询时order by与group by合用无效的解决办法

mysql5.7基础 select...order by...asc 按照一个字段进行升序排序

MySQL5.7:[Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause and contains

MySQL5.7:[Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause and contains

MySQL5.7:[Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause and contains

mysql5.7基础 select...order by...desc 按照一个字段进行降序排列