1. order by优化
什么是using filesort、 using idex?
create index idx_user_age_phone on tb_user(age,phone);
出现using idex说明了排序效率比较高。因为它直接通过索引给我们返回了有序数据。
1.1 违背最左前缀法则
如果索引排序违背了最左前缀法则,也会出现using filesort
1.2 默认索引排序为升序降序排也会using filesort
创建的索引当中默认是升序排序所以,虽然使用了索引但是如果是倒序排序的话还是会出现using filesort的情况。
如果想优化掉这个using filesort
create index idx_user_age_pho_ad on tb_user(age asc,phone desc);
1.3 小结
如果在大数据量排序的时候出现了filesort 可以将这个参数的大小往上调。增加缓冲区的空间,避免占用磁盘 提升sql效率。
show variables like 'sort_buffer_size';