学习笔记(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 拼起来,不要跨列使用)

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值