學習筆記(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 拼起來,不要跨列使用)

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章