大量實例助你看懂Explain的輸出內容,輕鬆搞定慢查詢mysql
EXPLAIN:查看SQL語句的執行計劃算法
EXPLAIN命令能夠幫助咱們深刻了解MySQL基於開銷的優化器,還能夠得到不少可能被優化器考慮到的訪問策略的細節,以及當運行SQL語句時哪一種策略預計會被優化器採用,在優化慢查詢時很是有用sql
執行explain以後結果集包含以下信息bash
+----+-------------+-------+------------+------+---------------+------+---------+------+--------+----------+-------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+------+---------------+------+---------+------+--------+----------+-------+
複製代碼
下面將對每個值進行解釋mysql優化
id用來標識整個查詢中SELELCT語句的順序,在嵌套查詢中id越大的語句越先執行,該值可能爲NULL運維
id若是相同,從上往下依次執行。id不一樣,id值越大,執行優先級越高,若是行引用其餘行的並集結果,則該值能夠爲NULLoop
select_type表示查詢使用的類型,有下面幾種:性能
simple: 簡單的select查詢,沒有union或者子查詢優化
mysql> explain select * from test where id = 1000;
+----+-------------+-------+------------+-------+---------------+---------+---------+-------+------+----------+-------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+-------+---------------+---------+---------+-------+------+----------+-------+
| 1 | SIMPLE | test | NULL | const | PRIMARY | PRIMARY | 4 | const | 1 | 100.00 | NULL |
+----+-------------+-------+------------+-------+---------------+---------+---------+-------+------+----------+-------+
複製代碼
primary: 最外層的select查詢ui
mysql> explain select * from (select * from test where id = 1000) a;
+----+-------------+------------+--------+---------------+---------+---------+-------+------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+------------+--------+---------------+---------+---------+-------+------+-------+
| 1 | PRIMARY | <derived2> | system | NULL | NULL | NULL | NULL | 1 | NULL |
| 2 | DERIVED | test | const | PRIMARY | PRIMARY | 8 | const | 1 | NULL |
+----+-------------+------------+--------+---------------+---------+---------+-------+------+-------+
複製代碼
union: union中的第二個或隨後的select查詢,不依賴於外部查詢的結果集
mysql> explain select * from test where id = 1000 union all select * from test2 ;
+----+--------------+------------+-------+---------------+---------+---------+-------+-------+-----------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+--------------+------------+-------+---------------+---------+---------+-------+-------+-----------------+
| 1 | PRIMARY | test | const | PRIMARY | PRIMARY | 8 | const | 1 | NULL |
| 2 | UNION | test2 | ALL | NULL | NULL | NULL | NULL | 67993 | NULL |
| NULL | UNION RESULT | <union1,2> | ALL | NULL | NULL | NULL | NULL | NULL | Using temporary |
+----+--------------+------------+-------+---------------+---------+---------+-------+-------+-----------------+
複製代碼
dependent union: union中的第二個或隨後的select查詢,依賴於外部查詢的結果集
mysql> explain select * from test where id in (select id from test where id = 1000 union all select id from test2) ;
+----+--------------------+------------+--------+---------------+---------+---------+-------+-------+-----------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+--------------------+------------+--------+---------------+---------+---------+-------+-------+-----------------+
| 1 | PRIMARY | test | ALL | NULL | NULL | NULL | NULL | 68505 | Using where |
| 2 | DEPENDENT SUBQUERY | test | const | PRIMARY | PRIMARY | 8 | const | 1 | Using index |
| 3 | DEPENDENT UNION | test2 | eq_ref | PRIMARY | PRIMARY | 8 | func | 1 | Using index |
| NULL | UNION RESULT | <union2,3> | ALL | NULL | NULL | NULL | NULL | NULL | Using temporary |
+----+--------------------+------------+--------+---------------+---------+---------+-------+-------+-----------------+
複製代碼
subquery: 子查詢中的第一個select查詢,不依賴與外部查詢的結果集
mysql> explain select * from test where id = (select id from test where id = 1000);
+----+-------------+-------+-------+---------------+---------+---------+-------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+-------+---------------+---------+---------+-------+------+-------------+
| 1 | PRIMARY | test | const | PRIMARY | PRIMARY | 8 | const | 1 | NULL |
| 2 | SUBQUERY | test | const | PRIMARY | PRIMARY | 8 | const | 1 | Using index |
+----+-------------+-------+-------+---------------+---------+---------+-------+------+-------------+
複製代碼
dependent subquery: 子查詢中的第一個select查詢,依賴於外部查詢的結果集
mysql> explain select * from test where id in (select id from test where id = 1000 union all select id from test2) ;
+----+--------------------+------------+--------+---------------+---------+---------+-------+-------+-----------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+--------------------+------------+--------+---------------+---------+---------+-------+-------+-----------------+
| 1 | PRIMARY | test | ALL | NULL | NULL | NULL | NULL | 68505 | Using where |
| 2 | DEPENDENT SUBQUERY | test | const | PRIMARY | PRIMARY | 8 | const | 1 | Using index |
| 3 | DEPENDENT UNION | test2 | eq_ref | PRIMARY | PRIMARY | 8 | func | 1 | Using index |
| NULL | UNION RESULT | <union2,3> | ALL | NULL | NULL | NULL | NULL | NULL | Using temporary |
+----+--------------------+------------+--------+---------------+---------+---------+-------+-------+-----------------+
複製代碼
derived: 用於from子句中有子查詢的狀況,mysql會遞歸執行這些子查詢,此結果集放在臨時表中
mysql> explain select * from (select * from test2 where id = 1000)a;
+----+-------------+------------+--------+---------------+---------+---------+-------+------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+------------+--------+---------------+---------+---------+-------+------+-------+
| 1 | PRIMARY | <derived2> | system | NULL | NULL | NULL | NULL | 1 | NULL |
| 2 | DERIVED | test2 | const | PRIMARY | PRIMARY | 8 | const | 1 | NULL |
+----+-------------+------------+--------+---------------+---------+---------+-------+------+-------+
複製代碼
table用來表示輸出行所引用的表名
type表示訪問類型,下面依次解釋各類類型,類型順序從最好到最差排列
system: 表僅有一行,是const類型的一個特例
mysql> explain select * from (select * from test2 where id = 1000)a;
+----+-------------+------------+--------+---------------+---------+---------+-------+------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+------------+--------+---------------+---------+---------+-------+------+-------+
| 1 | PRIMARY | <derived2> | system | NULL | NULL | NULL | NULL | 1 | NULL |
| 2 | DERIVED | test2 | const | PRIMARY | PRIMARY | 8 | const | 1 | NULL |
+----+-------------+------------+--------+---------------+---------+---------+-------+------+-------+
複製代碼
由於子查詢只有一行數據,模擬了單表只有一行數據,此時type爲system
const: 肯定只有一行匹配的時候,mysql優化器會在查詢前讀取它而且只讀取一次,速度很是快
mysql> explain select * from test where id =1 ;
+----+-------------+-------+-------+---------------+---------+---------+-------+------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+-------+---------------+---------+---------+-------+------+-------+
| 1 | SIMPLE | test | const | PRIMARY | PRIMARY | 8 | const | 1 | NULL |
+----+-------------+-------+-------+---------------+---------+---------+-------+------+-------+
1 row in set (0.00 sec)
複製代碼
eq_ref: 對於每一個來自於前面的表的行組合,從該表中讀取一行,經常使用在一個索引是unique key或者primary key
mysql> explain select * from test,test2 where test.com_key=test2.com_key;
+----+-------------+-------+--------+---------------+--------------+---------+--------------------+-------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+--------+---------------+--------------+---------+--------------------+-------+-------+
| 1 | SIMPLE | test2 | ALL | IDX(com_key) | NULL | NULL | NULL | 67993 | NULL |
| 1 | SIMPLE | test | eq_ref | IDX(com_key) | IDX(com_key) | 194 | test.test2.com_key | 1 | NULL |
+----+-------------+-------+--------+---------------+--------------+---------+--------------------+-------+-------+
複製代碼
ref: 對於來自前面的表的行組合,全部有匹配索引值的行都從這張表中讀取,若是聯接只使用鍵的最左邊的前綴,或若是鍵不是UNIQUE或PRIMARY KEY(換句話說,若是聯接不能基於關鍵字選擇單個行的話),則使用ref
ref能夠用於使用=或<=>操做符的帶索引的列
mysql> explain select * from test ,test2 where test.bnet_id=test2.aid;
+----+-------------+-------+------+---------------+---------+---------+-------------------+-------+-----------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+---------------+---------+---------+-------------------+-------+-----------------------+
| 1 | SIMPLE | test | ALL | NULL | NULL | NULL | NULL | 68505 | Using where |
| 1 | SIMPLE | test2 | ref | idx_aid | idx_aid | 5 | test.test.bnet_id | 34266 | Using index condition |
+----+-------------+-------+------+---------------+---------+---------+-------------------+-------+-----------------------+
複製代碼
test表bnet_id
不是索引,test2表aid
爲索引列
ref_or_null: 相似ref,可是添加了能夠專門搜索null值的行
mysql> explain select * from test where bnet_id=1 or bnet_id is null;
+----+-------------+-------+-------------+---------------+----------+---------+-------+------+-----------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+-------------+---------------+----------+---------+-------+------+-----------------------+
| 1 | SIMPLE | test | ref_or_null | idx_bnet | idx_bnet | 9 | const | 2 | Using index condition |
+----+-------------+-------+-------------+---------------+----------+---------+-------+------+-----------------------+
複製代碼
前提爲bnet_id
列爲索引,且bnet_id
列有null值
index_merge: 該訪問類型使用了索引合併優化方法,key列包含了使用的索引的清單,key_len包含了使用的索引的最長的關鍵元素
mysql> explain select * from test where id = 1 or bnet_id = 1;
+----+-------------+-------+-------------+------------------+------------------+---------+------+------+--------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+-------------+------------------+------------------+---------+------+------+--------------------------------------------+
| 1 | SIMPLE | test | index_merge | PRIMARY,idx_bnet | PRIMARY,idx_bnet | 8,9 | NULL | 2 | Using union(PRIMARY,idx_bnet); Using where |
+----+-------------+-------+-------------+------------------+------------------+---------+------+------+--------------------------------------------+
複製代碼
前提條件爲id
列和bnet_id
列都有單列索引。若是出現index_merge,而且這類SQL後期使用較頻繁,能夠考慮把單列索引換爲組合索引,這樣效率更高
range: 只檢索給定範圍的行,使用一個索引來選擇行。key列顯示使用了哪一個索引。key_len包含所使用索引的最長關鍵元素。在該類型中ref列爲NULL
當使用=、<>、>、>=、<、<=、IS NULL、<=>、BETWEEN或者IN操做符,用常量比較關鍵字列時,可使用range
mysql> explain select * from test where bnet_id > 1000 and bnet_id < 10000;
+----+-------------+-------+-------+---------------+----------+---------+------+------+-----------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+-------+---------------+----------+---------+------+------+-----------------------+
| 1 | SIMPLE | test | range | idx_bnet | idx_bnet | 9 | NULL | 1 | Using index condition |
+----+-------------+-------+-------+---------------+----------+---------+------+------+-----------------------+
複製代碼
前提條件爲bnet_id
列有索引
index: 在進行統計時很是常見,此聯接類型實際上會掃描索引樹
mysql> explain select count(*) from test;
+----+-------------+-------+-------+---------------+----------+---------+------+-------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+-------+---------------+----------+---------+------+-------+-------------+
| 1 | SIMPLE | test | index | NULL | idx_bnet | 9 | NULL | 68505 | Using index |
+----+-------------+-------+-------+---------------+----------+---------+------+-------+-------------+
複製代碼
all: 對於每一個來自於先前的表的行組合,進行完整的表掃描,一般能夠增長更多的索引而不要使用ALL,使得行能基於前面的表中的常數值或列值被檢索出
mysql> explain select * from test where create_time = '0000-00-00 00:00:00';
+----+-------------+-------+------+---------------+------+---------+------+-------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+---------------+------+---------+------+-------+-------------+
| 1 | SIMPLE | test | ALL | NULL | NULL | NULL | NULL | 68505 | Using where |
+----+-------------+-------+------+---------------+------+---------+------+-------+-------------+
複製代碼
possible_keys是指在這個SQL中,mysql可使用這個索引去輔助查找記錄,當查詢涉及到的字段,都會被列出,但不必定被查詢使用.若爲空則表示沒有可使用的索引,此時能夠經過檢查where語句看是否能夠引用某些列或者新建索引來提升性能。
key列顯示的是當前表實際使用的索引,若是沒有選擇索引,則此列爲null,要想強制MySQL使用或忽視possible_keys列中的索引,在查詢中使用FORCE INDEX、USE INDEX或者IGNORE INDEX
key_len列顯示MySQL決定使用的鍵長度。若是KEY鍵是NULL,則長度爲NULL。在不損失精確性的狀況下,長度越短越好
key len的長度還和字符集有關,latin1一個字符佔用1個字節,gbk一個字符佔用2個字節,utf8一個字符佔用3個字節。key_len的計算法方法:
列類型 | KEY_LEN | 備註 |
---|---|---|
id int | key_len = 4+1 | int爲4bytes,容許爲NULL,加1byte |
id bigint not null | key_len=8 | bigint爲8bytes |
user char(30) utf8 | key_len=30*3+1 | utf8每一個字符爲3bytes,容許爲NULL,加1byte |
user varchar(30) not null utf8 | key_len=30*3+2 | utf8每一個字符爲3bytes,變長數據類型,加2bytes |
user varchar(30) utf8 | key_len=30*3+2+1 | utf8每一個字符爲3bytes,容許爲NULL,加1byte,變長數據類型,加2bytes |
detail text(10) utf8 | key_len=30*3+2+1 | TEXT截取部分,被視爲動態列類型。 |
key_len只指示了where中用於條件過濾時被選中的索引列,是不包含order by
或group by
這一部分被選中的索引列
ref列用來顯示使用哪一個列或常數與key一塊兒從表中選擇相應的行。它顯示的列的名字(或const),此列多數時候爲null
rows列顯示的是mysql解析器認爲執行此SQL時必須掃描的行數。此數值爲一個預估值,不是具體值,一般比實際值小
此參數爲mysql 5.7 新加參數,指的是返回結果的行數所佔須要讀到的行(rows的值)的比例 對於使用join時,前一個表的結果集大小直接影響了循環的行數
extra表示不在其餘列而且也很重要的額外信息
using index: 該值表示這個SQL語句使用了覆蓋索引(覆蓋索引是指能夠直接在索引列中獲得想要的結果,而不用去回表),此時效率最高
mysql> explain select id from test;
+----+-------------+-------+-------+---------------+----------+---------+------+-------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+-------+---------------+----------+---------+------+-------+-------------+
| 1 | SIMPLE | test | index | NULL | idx_bnet | 9 | NULL | 68505 | Using index |
+----+-------------+-------+-------+---------------+----------+---------+------+-------+-------------+
複製代碼
這個例子中id
字段爲主鍵,可是key那裏顯示走的並非主鍵索引,這個是由於mysql的全部二級索引中都會包含全部的主鍵信息,而mysql沒有單獨的存儲主鍵索引,因此掃描二級索引的開銷比全表掃描更快
using where: 表示存儲引擎搜到記錄後進行了後過濾(POST-FILTER),若是查詢未能使用索引,using where的做用只是提醒咱們mysql要用where條件過濾結果集
mysql> explain select * from test where id > 1;
+----+-------------+-------+-------+---------------+---------+---------+------+-------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+-------+---------------+---------+---------+------+-------+-------------+
| 1 | SIMPLE | test | range | PRIMARY | PRIMARY | 8 | NULL | 34252 | Using where |
+----+-------------+-------+-------+---------------+---------+---------+------+-------+-------------+
複製代碼
using temporary 表示mysql須要使用臨時表來存儲結果集,常見於排序和分組查詢
mysql> explain select * from test where id in (1,2) group by bnet_id;
+----+-------------+-------+-------+-----------------------------------------+---------+---------+------+------+----------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+-------+-----------------------------------------+---------+---------+------+------+----------------------------------------------+
| 1 | SIMPLE | test | range | PRIMARY,IDX(event_key-bnet_Id),idx_bnet | PRIMARY | 8 | NULL | 2 | Using where; Using temporary; Using filesort |
+----+-------------+-------+-------+-----------------------------------------+---------+---------+------+------+----------------------------------------------+
複製代碼
using filesort: 是指mysql沒法利用索引直接完成排序(排序的字段不是索引字段),此時會用到緩衝空間來進行排序
mysql> explain select * from test order by bnet_id;
+----+-------------+-------+------+---------------+------+---------+------+-------+----------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+---------------+------+---------+------+-------+----------------+
| 1 | SIMPLE | test | ALL | NULL | NULL | NULL | NULL | 68505 | Using filesort |
+----+-------------+-------+------+---------------+------+---------+------+-------+----------------+
複製代碼
using join buffer: 強調在獲取鏈接條件時沒有用到索引,而且須要鏈接緩衝區來存儲中間結果。(性能能夠經過添加索引或者修改鏈接字段改進)
mysql> explain select * from test left join test2 on test.create_time = test2.create_time;
+----+-------------+-------+------------+------+---------------+------+---------+------+--------+----------+----------------------------------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+------+---------------+------+---------+------+--------+----------+----------------------------------------------------+
| 1 | SIMPLE | test | NULL | ALL | NULL | NULL | NULL | NULL | 959692 | 100.00 | NULL |
| 1 | SIMPLE | test2 | NULL | ALL | NULL | NULL | NULL | NULL | 958353 | 100.00 | Using where; Using join buffer (Block Nested Loop) |
+----+-------------+-------+------------+------+---------------+------+---------+------+--------+----------+----------------------------------------------------+
2 rows in set, 1 warning (0.00 sec)
複製代碼
Block Nested Loop是指Block Nested-Loop Join算法:將外層循環的行/結果集存入join buffer, 內層循環的每一行與整個buffer中的記錄作比較,從而減小內層循環的次數.
impossible where: 表示where條件致使沒有返回的行
mysql> explain select * from test where id is null;
+----+-------------+-------+------+---------------+------+---------+------+------+------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+---------------+------+---------+------+------+------------------+
| 1 | SIMPLE | NULL | NULL | NULL | NULL | NULL | NULL | NULL | Impossible WHERE |
+----+-------------+-------+------+---------------+------+---------+------+------+------------------+
複製代碼
using index condition: 是mysql 5.6 以後新加的特性,結合mysql的ICP(Index Condition Pushdown)特性使用。主要是優化了能夠在索引(僅限二級索引)上進行 like 查找
若是extra中出現多個上面結果,則表示順序使用上面的方法進行解析查詢
相關文章推薦閱讀: