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 對比和說明: