SQL优化实战
1. Order by优化
在Order by中,如果排序会造成文件排序(在磁盘中完成排序,性能比较差),就说明没有命中索引,这个时候可以使用最左前缀法则,让排序遵循最左前缀法则,避免文件排序
1)ORDER BY的字段改到一种表、不要夸表(设计表结构时需注意这一点)
2)OEDER BY字段建索引、多个字段时建联合索引(联合索引的字段顺序要与ORSER BY中的字段顺序一致)
3)ORDER BY中字段中联合索引的所有字段DESC或ASC要统一,否则索引不起作用
--- name,age,position 均为索引列
--- 没有使用文件查找
Explain select * from employees where name='customer' and position='dev' order by age position
--- 使用文件查找 using fliesort
Explain select * from employees where name='customer' order by position
--- 没有使用文件排序
Explain select * from employees where name='customer' order by age,position
--- 不满足最左前缀法则,使用using fliesort
Explain select * from employees where name='customer' order by position,age
--- 满足最左前缀法则使用索引排序
Exp