建立复合索引(age,birth),使用order by排序产生filesort情况
情况1:Select *from user where age>20 order by age;没有filesort,用到了index索引,原因是age是索引头
情况2:Select *from user where age>20 order by age, birth;没有filesort且用到索引
情况3:Select *from user where age>20 order by birth;产生了filesort,复合索引顺序为age, birth
情况4:Select *from user where age>20 order by birth, age;产生了filesort,复合索引顺序为age,name,顺序异常
情况5:Select *from user order by name;产生了filesort,复合索引顺序为age, birth,缺少age
情况6:Select *from user where birth >’2012-01-02’ order by birth; 产生了filesort,复合索引顺序为age, birth,缺少age
情况7:Select *from user where birth >’2012-01-02’ order by age; 没有filesort且用到索引
情况8:Select *from user order by age.ASC,birth.DESC;产生filesort,原因为默认为降序,aes违反了mysql规则,age默认为DESC降序,强制ASC升序
建议:建立好索引后,相当于一次排序,再order by使用到索引字段,再排序,提高查询效率
提示:出现filesort是不好的对一个sql(mysql会对数据使用一个外部的索引排序,不是按照表内索引顺序读取,mysql中无法利用索引完成的排序操作叫文件排序(言外之意:没有按照内部规则执行,mysql另起炉灶排序,危险))