mysql explain用法和結果的含義
重點是第二種用法,需要深入的瞭解。
先看一個例子:
- mysql> explain select * from t_order;
- +----+-------------+---------+------+---------------+------+---------+------+--------+-------+
- | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
- +----+-------------+---------+------+---------------+------+---------+------+--------+-------+
- | 1 | SIMPLE | t_order | ALL | NULL | NULL | NULL | NULL | 100453 | |
- +----+-------------+---------+------+---------------+------+---------+------+--------+-------+
- 1 row in set (0.03 sec)
加上extended後之後:
- mysql> explain extended select * from t_order;
- +----+-------------+---------+------+---------------+------+---------+------+--------+----------+-------+
- | id | select_type | table | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
- +----+-------------+---------+------+---------------+------+---------+------+--------+----------+-------+
- | 1 | SIMPLE | t_order | ALL | NULL | NULL | NULL | NULL | 100453 | 100.00 | |
- +----+-------------+---------+------+---------------+------+---------+------+--------+----------+-------+
- 1 row in set, 1 warning (0.00 sec)
有必要解釋一下這個長長的表格裡每一列的含義:
id | SELECT識別符。這是SELECT的查詢序列號 |
select_type |
SELECT型別,可以為以下任何一種:
|
table |
輸出的行所引用的表 |
type |
聯接型別。下面給出各種聯接型別,按照從最佳型別到最壞型別進行排序:
|
possible_keys |
指出MySQL能使用哪個索引在該表中找到行 |
key | 顯示MySQL實際決定使用的鍵(索引)。如果沒有選擇索引,鍵是NULL。 |
key_len | 顯示MySQL決定使用的鍵長度。如果鍵是NULL,則長度為NULL。 |
ref | 顯示使用哪個列或常數與key一起從表中選擇行。 |
rows | 顯示MySQL認為它執行查詢時必須檢查的行數。多行之間的資料相乘可以估算要處理的行數。 |
filtered | 顯示了透過條件過濾出的行數的百分比估計值。 |
Extra |
該列包含MySQL解決查詢的詳細資訊
|
一.select_type的說明
1.UNION:
當透過union來連線多個查詢結果時,第二個之後的select其select_type為UNION。
- mysql> explain select * from t_order where order_id=100 union select * from t_order where order_id=200;
- +----+--------------+------------+-------+---------------+---------+---------+-------+------+-------+
- | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
- +----+--------------+------------+-------+---------------+---------+---------+-------+------+-------+
- | 1 | PRIMARY | t_order | const | PRIMARY | PRIMARY | 4 | const | 1 | |
- | 2 | UNION | t_order | const | PRIMARY | PRIMARY | 4 | const | 1 | |
- | NULL | UNION RESULT | <union1,2> | ALL | NULL | NULL | NULL | NULL | NULL | |
- +----+--------------+------------+-------+---------------+---------+---------+-------+------+-------+
- 3 rows in set (0.34 sec)
2.DEPENDENT UNION與DEPENDENT SUBQUERY:
當union作為子查詢時,其中第二個union的select_type就是DEPENDENT UNION。
第一個子查詢的select_type則是DEPENDENT SUBQUERY。
- mysql> explain select * from t_order where order_id in (select order_id from t_order where order_id=100 union select order_id from t_order where order_id=200);
- +----+--------------------+------------+-------+---------------+---------+---------+-------+--------+-------------+
- | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
- +----+--------------------+------------+-------+---------------+---------+---------+-------+--------+-------------+
- | 1 | PRIMARY | t_order | ALL | NULL | NULL | NULL | NULL | 100453 | Using where |
- | 2 | DEPENDENT SUBQUERY | t_order | const | PRIMARY | PRIMARY | 4 | const | 1 | Using index |
- | 3 | DEPENDENT UNION | t_order | const | PRIMARY | PRIMARY | 4 | const | 1 | Using index |
- | NULL | UNION RESULT | <union2,3> | ALL | NULL | NULL | NULL | NULL | NULL | |
- +----+--------------------+------------+-------+---------------+---------+---------+-------+--------+-------------+
- 4 rows in set (0.03 sec)
3.SUBQUERY:
子查詢中的第一個select其select_type為SUBQUERY。
- mysql> explain select * from t_order where order_id=(select order_id from t_order where order_id=100);
- +----+-------------+---------+-------+---------------+---------+---------+-------+------+-------------+
- | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
- +----+-------------+---------+-------+---------------+---------+---------+-------+------+-------------+
- | 1 | PRIMARY | t_order | const | PRIMARY | PRIMARY | 4 | const | 1 | |
- | 2 | SUBQUERY | t_order | const | PRIMARY | PRIMARY | 4 | | 1 | Using index |
- +----+-------------+---------+-------+---------------+---------+---------+-------+------+-------------+
- 2 rows in set (0.03 sec)
4.DERIVED:
當子查詢是from子句時,其select_type為DERIVED。
- mysql> explain select * from (select order_id from t_order where order_id=100) a;
- +----+-------------+------------+--------+---------------+---------+---------+------+------+-------------+
- | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
- +----+-------------+------------+--------+---------------+---------+---------+------+------+-------------+
- | 1 | PRIMARY | <derived2> | system | NULL | NULL | NULL | NULL | 1 | |
- | 2 | DERIVED | t_order | const | PRIMARY | PRIMARY | 4 | | 1 | Using index |
- +----+-------------+------------+--------+---------------+---------+---------+------+------+-------------+
- 2 rows in set (0.03 sec)
二.type的說明
1.system,const
見上面4.DERIVED的例子。其中第一行的type就是為system,第二行是const,這兩種聯接型別是最快的。
2.eq_ref
在t_order表中的order_id是主鍵,t_order_ext表中的order_id也是主鍵,該表可以認為是訂單表的補充資訊表,他們的關係是1對1,在下面的例子中可以看到b表的連線型別是eq_ref,這是極快的聯接型別。
- mysql> explain select * from t_order a,t_order_ext b where a.order_id=b.order_id;
- +----+-------------+-------+--------+---------------+---------+---------+-----------------+------+-------------+
- | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
- +----+-------------+-------+--------+---------------+---------+---------+-----------------+------+-------------+
- | 1 | SIMPLE | b | ALL | order_id | NULL | NULL | NULL | 1 | |
- | 1 | SIMPLE | a | eq_ref | PRIMARY | PRIMARY | 4 | test.b.order_id | 1 | Using where |
- +----+-------------+-------+--------+---------------+---------+---------+-----------------+------+-------------+
- 2 rows in set (0.00 sec)
3.ref
下面的例子在上面的例子上略作了修改,加上了條件。此時b表的聯接型別變成了ref。因為所有與a表中order_id=100的匹配記錄都將會從b表獲取。這是比較常見的聯接型別。
- mysql> explain select * from t_order a,t_order_ext b where a.order_id=b.order_id and a.order_id=100;
- +----+-------------+-------+-------+---------------+----------+---------+-------+------+-------+
- | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
- +----+-------------+-------+-------+---------------+----------+---------+-------+------+-------+
- | 1 | SIMPLE | a | const | PRIMARY | PRIMARY | 4 | const | 1 | |
- | 1 | SIMPLE | b | ref | order_id | order_id | 4 | const | 1 | |
- +----+-------------+-------+-------+---------------+----------+---------+-------+------+-------+
- 2 rows in set (0.00 sec)
4.ref_or_null
user_id欄位是一個可以為空的欄位,並對該欄位建立了一個索引。在下面的查詢中可以看到聯接型別為ref_or_null,這是mysql為含有null的欄位專門做的處理。在我們的表設計中應當儘量避免索引欄位為NULL,因為這會額外的耗費mysql的處理時間來做最佳化。
- mysql> explain select * from t_order where user_id=100 or user_id is null;
- +----+-------------+---------+-------------+---------------+---------+---------+-------+-------+-------------+
- | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
- +----+-------------+---------+-------------+---------------+---------+---------+-------+-------+-------------+
- | 1 | SIMPLE | t_order | ref_or_null | user_id | user_id | 5 | const | 50325 | Using where |
- +----+-------------+---------+-------------+---------------+---------+---------+-------+-------+-------------+
- 1 row in set (0.00 sec)
5.index_merge
經常出現在使用一張表中的多個索引時。mysql會將多個索引合併在一起,如下例:
- mysql> explain select * from t_order where order_id=100 or user_id=10;
- +----+-------------+---------+-------------+-----------------+-----------------+---------+------+------+-------------------------------------------+
- | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
- +----+-------------+---------+-------------+-----------------+-----------------+---------+------+------+-------------------------------------------+
- | 1 | SIMPLE | t_order | index_merge | PRIMARY,user_id | PRIMARY,user_id | 4,5 | NULL | 2 | Using union(PRIMARY,user_id); Using where |
- +----+-------------+---------+-------------+-----------------+-----------------+---------+------+------+-------------------------------------------+
- 1 row in set (0.09 sec)
6.unique_subquery
該聯接型別用於替換value IN (SELECT primary_key FROM single_table WHERE some_expr)這樣的子查詢的ref。注意ref列,其中第二行顯示的是func,表明unique_subquery是一個函式,而不是一個普通的ref。
- mysql> explain select * from t_order where order_id in (select order_id from t_order where user_id=10);
- +----+--------------------+---------+-----------------+-----------------+---------+---------+------+--------+-------------+
- | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
- +----+--------------------+---------+-----------------+-----------------+---------+---------+------+--------+-------------+
- | 1 | PRIMARY | t_order | ALL | NULL | NULL | NULL | NULL | 100649 | Using where |
- | 2 | DEPENDENT SUBQUERY | t_order | unique_subquery | PRIMARY,user_id | PRIMARY | 4 | func | 1 | Using where |
- +----+--------------------+---------+-----------------+-----------------+---------+---------+------+--------+-------------+
- 2 rows in set (0.00 sec)
7.index_subquery
該聯接型別與上面的太像了,唯一的差別就是子查詢查的不是主鍵而是非唯一索引。
- mysql> explain select * from t_order where user_id in (select user_id from t_order where order_id>10);
- +----+--------------------+---------+----------------+-----------------+---------+---------+------+--------+--------------------------+
- | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
- +----+--------------------+---------+----------------+-----------------+---------+---------+------+--------+--------------------------+
- | 1 | PRIMARY | t_order | ALL | NULL | NULL | NULL | NULL | 100649 | Using where |
- | 2 | DEPENDENT SUBQUERY | t_order | index_subquery | PRIMARY,user_id | user_id | 5 | func | 50324 | Using index; Using where |
- +----+--------------------+---------+----------------+-----------------+---------+---------+------+--------+--------------------------+
- 2 rows in set (0.00 sec)
8.range
按指定的範圍進行檢索,很常見。
- mysql> explain select * from t_order where user_id in (100,200,300);
- +----+-------------+---------+-------+---------------+---------+---------+------+------+-------------+
- | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
- +----+-------------+---------+-------+---------------+---------+---------+------+------+-------------+
- | 1 | SIMPLE | t_order | range | user_id | user_id | 5 | NULL | 3 | Using where |
- +----+-------------+---------+-------+---------------+---------+---------+------+------+-------------+
- 1 row in set (0.00 sec)
9.index
在進行統計時非常常見,此聯接型別實際上會掃描索引樹,僅比ALL快些。
- mysql> explain select count(*) from t_order;
- +----+-------------+---------+-------+---------------+---------+---------+------+--------+-------------+
- | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
- +----+-------------+---------+-------+---------------+---------+---------+------+--------+-------------+
- | 1 | SIMPLE | t_order | index | NULL | user_id | 5 | NULL | 100649 | Using index |
- +----+-------------+---------+-------+---------------+---------+---------+------+--------+-------------+
- 1 row in set (0.00 sec)
10.ALL
完整的掃描全表,最慢的聯接型別,儘可能的避免。
- mysql> explain select * from t_order;
- +----+-------------+---------+------+---------------+------+---------+------+--------+-------+
- | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
- +----+-------------+---------+------+---------------+------+---------+------+--------+-------+
- | 1 | SIMPLE | t_order | ALL | NULL | NULL | NULL | NULL | 100649 | |
- +----+-------------+---------+------+---------------+------+---------+------+--------+-------+
- 1 row in set (0.00 sec)
三.extra的說明
1.Distinct
MySQL發現第1個匹配行後,停止為當前的行組合搜尋更多的行。對於此項沒有找到合適的例子,求指點。
2.Not exists
因為b表中的order_id是主鍵,不可能為NULL,所以mysql在用a表的order_id掃描t_order表,並查詢b表的行時,如果在b表發現一個匹配的行就不再繼續掃描b了,因為b表中的order_id欄位不可能為NULL。這樣避免了對b表的多次掃描。
- mysql> explain select count(1) from t_order a left join t_order_ext b on a.order_id=b.order_id where b.order_id is null;
- +----+-------------+-------+-------+---------------+--------------+---------+-----------------+--------+--------------------------------------+
- | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
- +----+-------------+-------+-------+---------------+--------------+---------+-----------------+--------+--------------------------------------+
- | 1 | SIMPLE | a | index | NULL | express_type | 1 | NULL | 100395 | Using index |
- | 1 | SIMPLE | b | ref | order_id | order_id | 4 | test.a.order_id | 1 | Using where; Using index; Not exists |
- +----+-------------+-------+-------+---------------+--------------+---------+-----------------+--------+--------------------------------------+
- 2 rows in set (0.01 sec)
3.Range checked for each record
這種情況是mysql沒有發現好的索引可用,速度比沒有索引要快得多。
- mysql> explain select * from t_order t, t_order_ext s where s.order_id>=t.order_id and s.order_id<=t.order_id and t.express_type>5;
- +----+-------------+-------+-------+----------------------+--------------+---------+------+------+------------------------------------------------+
- | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
- +----+-------------+-------+-------+----------------------+--------------+---------+------+------+------------------------------------------------+
- | 1 | SIMPLE | t | range | PRIMARY,express_type | express_type | 1 | NULL | 1 | Using where |
- | 1 | SIMPLE | s | ALL | order_id | NULL | NULL | NULL | 1 | Range checked for each record (index map: 0x1) |
- +----+-------------+-------+-------+----------------------+--------------+---------+------+------+------------------------------------------------+
- 2 rows in set (0.00 sec)
4.Using filesort
在有排序子句的情況下很常見的一種情況。此時mysql會根據聯接型別瀏覽所有符合條件的記錄,並儲存排序關鍵字和行指標,然後排序關鍵字並按順序檢索行。
- mysql> explain select * from t_order order by express_type;
- +----+-------------+---------+------+---------------+------+---------+------+--------+----------------+
- | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
- +----+-------------+---------+------+---------------+------+---------+------+--------+----------------+
- | 1 | SIMPLE | t_order | ALL | NULL | NULL | NULL | NULL | 100395 | Using filesort |
- +----+-------------+---------+------+---------------+------+---------+------+--------+----------------+
- 1 row in set (0.00 sec)
5.Using index
這是效能很高的一種情況。當查詢所需的資料可以直接從索引樹中檢索到時,就會出現。上面的例子中有很多這樣的例子,不再多舉例了。
6.Using temporary
發生這種情況一般都是需要進行最佳化的。mysql需要建立一張臨時表用來處理此類查詢。
- mysql> explain select * from t_order a left join t_order_ext b on a.order_id=b.order_id group by b.order_id;
- +----+-------------+-------+------+---------------+----------+---------+-----------------+--------+---------------------------------+
- | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
- +----+-------------+-------+------+---------------+----------+---------+-----------------+--------+---------------------------------+
- | 1 | SIMPLE | a | ALL | NULL | NULL | NULL | NULL | 100395 | Using temporary; Using filesort |
- | 1 | SIMPLE | b | ref | order_id | order_id | 4 | test.a.order_id | 1 | |
- +----+-------------+-------+------+---------------+----------+---------+-----------------+--------+---------------------------------+
- 2 rows in set (0.00 sec)
7.Using where
當有where子句時,extra都會有說明。
8.Using sort_union(...)/Using union(...)/Using intersect(...)
下面的例子中user_id是一個檢索範圍,此時mysql會使用sort_union函式來進行索引的合併。而當user_id是一個固定值時,請參看上面type說明5.index_merge的例子,此時會使用union函式進行索引合併。
- mysql> explain select * from t_order where order_id=100 or user_id>10;
- +----+-------------+---------+-------------+-----------------+-----------------+---------+------+------+------------------------------------------------+
- | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
- +----+-------------+---------+-------------+-----------------+-----------------+---------+------+------+------------------------------------------------+
- | 1 | SIMPLE | t_order | index_merge | PRIMARY,user_id | user_id,PRIMARY | 5,4 | NULL | 2 | Using sort_union(user_id,PRIMARY); Using where |
- +----+-------------+---------+-------------+-----------------+-----------------+---------+------+------+------------------------------------------------+
- 1 row in set (0.00 sec)
對於Using intersect的例子可以參看下例,user_id與express_type發生了索引交叉合併。
- mysql> explain select * from t_order where express_type=1 and user_id=100;
- +----+-------------+---------+-------------+----------------------+----------------------+---------+------+------+----------------------------------------------------+
- | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
- +----+-------------+---------+-------------+----------------------+----------------------+---------+------+------+----------------------------------------------------+
- | 1 | SIMPLE | t_order | index_merge | user_id,express_type | user_id,express_type | 5,1 | NULL | 1 | Using intersect(user_id,express_type); Using where |
- +----+-------------+---------+-------------+----------------------+----------------------+---------+------+------+----------------------------------------------------+
- 1 row in set (0.00 sec)
9.Using index for group-by
表明可以在索引中找到分組所需的所有資料,不需要查詢實際的表。
- mysql> explain select user_id from t_order group by user_id;
- +----+-------------+---------+-------+---------------+---------+---------+------+------+--------------------------+
- | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
- +----+-------------+---------+-------+---------------+---------+---------+------+------+--------------------------+
- | 1 | SIMPLE | t_order | range | NULL | user_id | 5 | NULL | 3 | Using index for group-by |
- +----+-------------+---------+-------+---------------+---------+---------+------+------+--------------------------+
- 1 row in set (0.00 sec)
除了上面的三個說明,還需要注意rows的數值,多行之間的數值是乘積的關係,可以估算大概要處理的行數,如果乘積很大,那就很有最佳化的必要了。
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/28939273/viewspace-2140609/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- explain結果含義AI
- EXPLAIN結果含義(轉)AI
- MySQL的EXPLAIN命令結果詳解MySqlAI
- Explain各列的含義AI
- explain中filesort含義AI
- MySQL的Explain結果輸出項解釋MySqlAI
- async 函式的含義和用法函式
- Thunk 函式的含義和用法函式
- MySQL EXPLAIN結果集分析 - 附帶大量案例MySqlAI
- co 函式庫的含義和用法函式
- MySQL執行計劃explain輸出列結果解析MySqlAI
- mysql 下的提示和含義MySql
- MySQL 的 EXPLAIN 語句及用法MySqlAI
- mysql效能分析之explain的用法MySqlAI
- MySQL中explain的幾點用法MySqlAI
- Mysql效能最佳化(三)--explain返回的結果說明MySqlAI
- rhel5 執行who -r的結果含義
- [MySql]explain用法及實踐MySqlAI
- MySQL的Explain總結MySqlAI
- Promise含義及基本用法Promise
- Generator 函式的含義與用法函式
- explain plan 的用法AI
- MySQL show status關鍵結果釋義和提升效能建議MySql
- oracle explain plan for的用法OracleAI
- 卡卡西:一文詳解explain各欄位含義AI
- 舉例解釋一下explain各欄位的含義AI
- 結構體定義 typedef struct 用法詳解和用法小結結構體Struct
- Oracle EXPLAIN PLAN用法OracleAI
- MySQL explain結果Extra中"Using Index"與"Using where; Using index"區別MySqlAIIndex
- Python 中 key 引數的含義及用法Python
- Python中key引數的含義及用法Python
- 命令注入-命令的連線符【‘&’‘&&’‘||’‘|’】的含義及其用法
- SMART原則的定義和含義
- 【執行計劃】格式化EXPLAIN PLAN的輸出結果AI
- MySQL Explain的使用MySqlAI
- C++中&和*的含義C++
- MySQL explainMySqlAI
- [Mysql]ExplainMySqlAI