MySQL EXPLAIN結果集分析 – 附帶大量案例
- 2019 年 10 月 3 日
- 筆記
大量實例助你看懂Explain的輸出內容,輕鬆搞定慢查詢
EXPLAIN:查看SQL語句的執行計劃
EXPLAIN命令可以幫助我們深入了解MySQL基於開銷的優化器,還可以獲得很多可能被優化器考慮到的訪問策略的細節,以及當運行SQL語句時哪種策略預計會被優化器採用,在優化慢查詢時非常有用
執行explain之後結果集包含如下資訊
+----+-------------+-------+------------+------+---------------+------+---------+------+--------+----------+-------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+-------+------------+------+---------------+------+---------+------+--------+----------+-------+
下面將對每一個值進行解釋
1、id
id用來標識整個查詢中SELELCT語句的順序,在嵌套查詢中id越大的語句越先執行,該值可能為NULL
id如果相同,從上往下依次執行。id不同,id值越大,執行優先順序越高,如果行引用其他行的並集結果,則該值可以為NULL
2、select_type
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查詢
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 | +----+-------------+------------+--------+---------------+---------+---------+-------+------+-------+
3、table
table用來表示輸出行所引用的表名
4、type(重要)
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 | +----+-------------+-------+------+---------------+------+---------+------+-------+-------------+
5、possible_keys
possible_keys是指在這個SQL中,mysql可以使用這個索引去輔助查找記錄,當查詢涉及到的欄位,都會被列出,但不一定被查詢使用.若為空則表示沒有可以使用的索引,此時可以通過檢查where語句看是否可以引用某些列或者新建索引來提高性能。
6、key(重要)
key列顯示的是當前表實際使用的索引,如果沒有選擇索引,則此列為null,要想強制MySQL使用或忽視possible_keys列中的索引,在查詢中使用FORCE INDEX、USE INDEX或者IGNORE INDEX
7、key_len
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
這一部分被選中的索引列
8、ref
ref列用來顯示使用哪個列或常數與key一起從表中選擇相應的行。它顯示的列的名字(或const),此列多數時候為null
9、rows
rows列顯示的是mysql解析器認為執行此SQL時必須掃描的行數。此數值為一個預估值,不是具體值,通常比實際值小
10、filtered
此參數為mysql 5.7 新加參數,指的是返回結果的行數所佔需要讀到的行(rows的值)的比例
對於使用join時,前一個表的結果集大小直接影響了循環的行數
11、extra(重要)
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中出現多個上面結果,則表示順序使用上面的方法進行解析查詢
相關文章推薦閱讀: