学习笔记(12):MySQL版SQL优化-优化示例

立即学习:https://edu.csdn.net/course/play/25283/297148?utm_source=blogtoedu

(ii)using  temporary 性能损耗大,用到了临时表。一般出现在group by 语句中,已经有表了,但不适用,必须再来一张表。解析过程:

 from .. on ..join .. where .. group by ...having ...select dinstinct ..order by limit ...

a.explain select * from test03 where a2=2 and a4=4 group by a2,a4;  ---没有 using  temporary

b.explain select * from test03 where a2=2 and a4=4 group by a3;   ---有 using  temporary

总结:i.如果(a,b,c,d)复合索引  和使用的顺序全部一致(且不跨列使用),则复合索引全部使用。

(iii) using index:性能提升;索引覆盖(覆盖索引)。原因:不读取原文件,只从索引文件中获取数据(不需要回表查询)

如果用到了索引覆盖(using index时),会对possible_keys和key造成影响;

a.如果没有where,则索引只出现在key中;

b.如果有where,则索引出现在key和possible_keys中

(iii).using where  (需要回表查询)

(iv)impossible where:where子句永远为false

explain select a1,a2,a3,a4 from test03 where a1=1 and a2=2 and a4=4 order by a3;-----以上SQL用到了a1,a2两个索引,该两个字段  不需要回表查询using index;而a4因为跨列使用,造成了该索引失效,需要回表查询

explain select a1,a2,a3,a4 from test03 where a1=1 and a4=4 order by a3;------以上SQL出现了using filesort(文件内排序,“多了一次额外的查找/排序”);不要跨列使用(where 和order by 拼起来,不要跨列使用)

发布了15 篇原创文章 · 获赞 4 · 访问量 631

猜你喜欢

转载自blog.csdn.net/ankang_66/article/details/104354744