MySQL優化學習

sql優化:
一、type:連接類型 最關鍵的一列 效率(const>eq_ref>ref>range>index>all)

1、const:查詢索引字段,並且表中最多隻有一行匹配(好像只有主鍵查詢只匹配一行纔會是const,有些情況唯一索引匹配一行會是ref)

2、eq_ref 主鍵或者唯一索引

3、ref 非唯一索引(主鍵也是唯一索引)

4、range 索引的範圍查詢

5、index (type=index extra = using index 代表索引覆蓋,即不需要回表)

6、all 全表掃描(通常沒有建索引的列)

二、key_len

索引的長度,在不損失精度的情況下越短越好

三、ref

四、rows (內循環的次數)

五、extra

重要的幾個

1、using temporary(組合查詢返回的數據量太大需要建立一個臨時表存儲數據,出現這個sql應該優化)

2、using where (where查詢條件)

3、using index(判斷是否僅使用索引查詢,使用索引樹並且不需要回表查詢)

4、using filesort(order by 太佔內存,使用文件排序)

瞭解的幾個

1、const row not found(據說是當表爲空的時候展示,我用了個空表explain之後發現extra列是空值)

2、deleting all rows (MYISAM存儲引擎快速清空表)

3、first_match(select * from a where name in(select a_name from B) ,B中有n條記錄都記錄了同一個a_name,每個a_name都只會匹配一次。exist也有同樣的效果)

4、impossible having, impssible where (錯誤的having 和where如,where 1<0)

5、Impossible WHERE noticed after reading const tables(如 where id =1 and name = “temp”,表中不存在id=1並且name=temp的記錄)
來自 https://www.cnblogs.com/man1s/p/10267669.html
or 附帶一個詳細的extra鏈接:https://blog.csdn.net/poxiaonie/article/details/77757471

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