mysql-为排序使用索引order by,group by

2020-02-26  本文已影响0人  有心人2021
一.排序原则的纲领
原则.png
二.case
CREATE TABLE `tblA`( 
  `id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT,  
   `age` INT(11) DEFAULT NULL,   
   `birth` timestamp not NULL 
)ENGINE=INNODB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
 
insert into tblA(age,birth) values (22,now());
insert into tblA(age,birth) values (23,now());
insert into tblA(age,birth) values (24,now());
 
create index idx_A_ageBirth on tblA(age,birth);
select * from tblA;

explain select * from tbla where age>20 order by age;
explain select * from tbla where age>20 order by age,birth;
explain select * from tbla where age>20 order by birth;
explain select * from tbla where age>20 order by birth,age;
explain select * from tbla order by birth;
explain select * from tbla where birth>'2018-10-05 12:00:00' order by birth;
explain select * from tbla where birth>'2018-10-05 12:00:00' order by age;
explain select * from tbla order by  age asc, birth desc;

分析.png
- ORDER BY a
- ORDER BY a,b
- ORDER BY a,b,c
- ORDER BY a DESC,b DESC,c DESC
-WHERE a = const  ORDER BY b,c
-WHERE a = const AND b = const ORDER BY c
-WHERE a = const  ORDER BY b,c
-WHERE a = const AND b > const ORDER BY b,c
-ORDER BY a ASC,b DESC,c DESC /*排序不一致*/
-WHERE g = const ORDER BY b,c    /*丢失a索引*/
-WHERE a = const ORDER BY c     /*丢失b索引*/
-WHERE a = const ORDER BY a,d /*d不是索引的一部分*/
-WHERE a in(...) ORDER BY b,c /*对于排序来说,多个相等的条件也是范围查询*/
二. 总结
order by.png

如果不在索引列上,filesort有两种算法:mysql就要启动双路排序和单路排序


FileSort两种方式.png

针对这种情况,要避免多次io:


image.png
提高order by速度.png group by优化策略
上一篇 下一篇

猜你喜欢

热点阅读