Mysql Explain 詳細引見。本站提示廣大學習愛好者:(Mysql Explain 詳細引見)文章只能為提供參考,不一定能成為您想要的結果。以下是Mysql Explain 詳細引見正文
Mysql Explain 這裡做一個材料的片面整理。
一.語法
explain < table_name >
例如: explain select * from t3 where id=3952602;
二.explain輸入解釋
+----+-------------+-------+-------+-------------------+---------+---------+-------+------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+-------+-------------------+---------+---------+-------+------+-------+
1.id
我的了解是SQL執行的順利的標識,SQL從大到小的執行.
例如:
mysql> explain select * from (select * from ( select * from t3 where id=3952602) a) b;
+----+-------------+------------+--------+-------------------+---------+---------+------+------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+------------+--------+-------------------+---------+---------+------+------+-------+
| 1 | PRIMARY | <derived2> | system | NULL | NULL | NULL | NULL | 1 | |
| 2 | DERIVED | <derived3> | system | NULL | NULL | NULL | NULL | 1 | |
| 3 | DERIVED | t3 | const | PRIMARY,idx_t3_id | PRIMARY | 4 | | 1 | |
+----+-------------+------------+--------+-------------------+---------+---------+------+------+-------+
很顯然這條SQL是從裡向外的執行,就是從id=3 向上執行.
2. select_type
就是select類型,可以有以下
(1) SIMPLE
復雜SELECT(不運用UNION或子查詢等) 例如:
mysql> explain select * from t3 where id=3952602;
+----+-------------+-------+-------+-------------------+---------+---------+-------+------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+-------+-------------------+---------+---------+-------+------+-------+
| 1 | SIMPLE | t3 | const | PRIMARY,idx_t3_id | PRIMARY | 4 | const | 1 | |
+----+-------------+-------+-------+-------------------+---------+---------+-------+------+-------+
(2). PRIMARY
我的了解是最外層的select.例如:
mysql> explain select * from (select * from t3 where id=3952602) 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 | t3 | const | PRIMARY,idx_t3_id | PRIMARY | 4 | | 1 | |
+----+-------------+------------+--------+-------------------+---------+---------+------+------+-------+
(3).UNION
UNION中的第二個或前面的SELECT語句.例如
mysql> explain select * from t3 where id=3952602 union all select * from t3 ;
+----+--------------+------------+-------+-------------------+---------+---------+-------+------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+--------------+------------+-------+-------------------+---------+---------+-------+------+-------+
| 1 | PRIMARY | t3 | const | PRIMARY,idx_t3_id | PRIMARY | 4 | const | 1 | |
| 2 | UNION | t3 | ALL | NULL | NULL | NULL | NULL | 1000 | |
|NULL | UNION RESULT | <union1,2> | ALL | NULL | NULL | NULL | NULL | NULL | |
+----+--------------+------------+-------+-------------------+---------+---------+-------+------+-------+
(4).DEPENDENT UNION
UNION中的第二個或前面的SELECT語句,取決於裡面的查詢
mysql> explain select * from t3 where id in (select id from t3 where id=3952602 union all select id from t3) ;
+----+--------------------+------------+--------+-------------------+---------+---------+-------+------+--------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+--------------------+------------+--------+-------------------+---------+---------+-------+------+--------------------------+
| 1 | PRIMARY | t3 | ALL | NULL | NULL | NULL | NULL | 1000 | Using where |
| 2 | DEPENDENT SUBQUERY | t3 | const | PRIMARY,idx_t3_id | PRIMARY | 4 | const | 1 | Using index |
| 3 | DEPENDENT UNION | t3 | eq_ref | PRIMARY,idx_t3_id | PRIMARY | 4 | func | 1 | Using where; Using index |
|NULL | UNION RESULT | <union2,3> | ALL | NULL | NULL | NULL | NULL | NULL | |
+----+--------------------+------------+--------+-------------------+---------+---------+-------+------+--------------------------+
(4).UNION RESULT
UNION的後果。
mysql> explain select * from t3 where id=3952602 union all select * from t3 ;
+----+--------------+------------+-------+-------------------+---------+---------+-------+------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+--------------+------------+-------+-------------------+---------+---------+-------+------+-------+
| 1 | PRIMARY | t3 | const | PRIMARY,idx_t3_id | PRIMARY | 4 | const | 1 | |
| 2 | UNION | t3 | ALL | NULL | NULL | NULL | NULL | 1000 | |
|NULL | UNION RESULT | <union1,2> | ALL | NULL | NULL | NULL | NULL | NULL | |
+----+--------------+------------+-------+-------------------+---------+---------+-------+------+-------+
(5).SUBQUERY
子查詢中的第一個SELECT.
mysql> explain select * from t3 where id = (select id from t3 where id=3952602 ) ;
+----+-------------+-------+-------+-------------------+---------+---------+-------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+-------+-------------------+---------+---------+-------+------+-------------+
| 1 | PRIMARY | t3 | const | PRIMARY,idx_t3_id | PRIMARY | 4 | const | 1 | |
| 2 | SUBQUERY | t3 | const | PRIMARY,idx_t3_id | PRIMARY | 4 | | 1 | Using index |
+----+-------------+-------+-------+-------------------+---------+---------+-------+------+-------------+
(6). DEPENDENT SUBQUERY
子查詢中的第一個SELECT,取決於裡面的查詢
mysql> explain select id from t3 where id in (select id from t3 where id=3952602 ) ;
+----+--------------------+-------+-------+-------------------+---------+---------+-------+------+--------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+--------------------+-------+-------+-------------------+---------+---------+-------+------+--------------------------+
| 1 | PRIMARY | t3 | index | NULL | PRIMARY | 4 | NULL | 1000 | Using where; Using index |
| 2 | DEPENDENT SUBQUERY | t3 | const | PRIMARY,idx_t3_id | PRIMARY | 4 | const | 1 | Using index |
+----+--------------------+-------+-------+-------------------+---------+---------+-------+------+--------------------------+
7).DERIVED
派生表的SELECT(FROM子句的子查詢)
mysql> explain select * from (select * from t3 where id=3952602) 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 | t3 | const | PRIMARY,idx_t3_id | PRIMARY | 4 | | 1 | |
+----+-------------+------------+--------+-------------------+---------+---------+------+------+-------+
3.table
顯示這一行的數據是關於哪張表的.
有時不是真實的表名字,看到的是derivedx(x是個數字,我的了解是第幾步執行的後果)
mysql> explain select * from (select * from ( select * from t3 where id=3952602) a) b;
+----+-------------+------------+--------+-------------------+---------+---------+------+------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+------------+--------+-------------------+---------+---------+------+------+-------+
| 1 | PRIMARY | <derived2> | system | NULL | NULL | NULL | NULL | 1 | |
| 2 | DERIVED | <derived3> | system | NULL | NULL | NULL | NULL | 1 | |
| 3 | DERIVED | t3 | const | PRIMARY,idx_t3_id | PRIMARY | 4 | | 1 | |
+----+-------------+------------+--------+-------------------+---------+---------+------+------+-------+
4.type
這列很重要,顯示了銜接運用了哪品種別,有無運用索引.
從最好到最差的銜接類型為const、eq_reg、ref、range、indexhe和ALL
(1).system
這是const聯接類型的一個特例。表僅有一行滿足條件.如下(t3表上的id是 primary key)
mysql> explain select * from (select * from t3 where id=3952602) 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 | t3 | const | PRIMARY,idx_t3_id | PRIMARY | 4 | | 1 | |
+----+-------------+------------+--------+-------------------+---------+---------+------+------+-------+
(2).const
表最多有一個婚配行,它將在查詢開端時被讀取。由於僅有一行,在這行的列值可被優化器剩余局部以為是常數。const表很快,由於它們只讀取一次!
const用於用常數值比擬PRIMARY KEY或UNIQUE索引的一切局部時。在上面的查詢中,tbl_name可以用於const表:
SELECT * from tbl_name WHERE primary_key=1;
SELECT * from tbl_name WHERE primary_key_part1=1和 primary_key_part2=2;
例如:
mysql> explain select * from t3 where id=3952602;
+----+-------------+-------+-------+-------------------+---------+---------+-------+------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+-------+-------------------+---------+---------+-------+------+-------+
| 1 | SIMPLE | t3 | const | PRIMARY,idx_t3_id | PRIMARY | 4 | const | 1 | |
+----+-------------+-------+-------+-------------------+---------+---------+-------+------+-------+
(3). eq_ref
關於每個來自於後面的表的行組合,從該表中讀取一行。這能夠是最好的聯接類型,除了const類型。它用在一個索引的一切局部被聯接運用並且索引是UNIQUE或PRIMARY KEY。
eq_ref可以用於運用= 操作符比擬的帶索引的列。比擬值可以為常量或一個運用在該表後面所讀取的表的列的表達式。
在上面的例子中,MySQL可以運用eq_ref聯接來處置ref_tables:
SELECT * FROM ref_table,other_table WHERE ref_table.key_column=other_table.column; SELECT * FROM ref_table,other_table WHERE ref_table.key_column_part1=other_table.column AND ref_table.key_column_part2=1;
例如
mysql> create unique index idx_t3_id on t3(id) ;
Query OK, 1000 rows affected (0.03 sec)
Records: 1000 Duplicates: 0 Warnings: 0
mysql> explain select * from t3,t4 where t3.id=t4.accountid;
+----+-------------+-------+--------+-------------------+-----------+---------+----------------------+------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+--------+-------------------+-----------+---------+----------------------+------+-------+
| 1 | SIMPLE | t4 | ALL | NULL | NULL | NULL | NULL | 1000 | |
| 1 | SIMPLE | t3 | eq_ref | PRIMARY,idx_t3_id | idx_t3_id | 4 | dbatest.t4.accountid | 1 | |
+----+-------------+-------+--------+-------------------+-----------+---------+----------------------+------+-------+
(4).ref
關於每個來自於後面的表的行組合,一切有婚配索引值的即將從這張表中讀取。假如聯接只運用鍵的最右邊的前綴,或假如鍵不是UNIQUE或PRIMARY KEY(換句話說,假如聯接不能基於關鍵字選擇單個行的話),則運用ref。假如運用的鍵僅僅婚配大批行,該聯接類型是不錯的。
ref可以用於運用=或<=>操作符的帶索引的列。
在上面的例子中,MySQL可以運用ref聯接來處置ref_tables:
SELECT * FROM ref_table WHERE key_column=expr; SELECT * FROM ref_table,other_table WHERE ref_table.key_column=other_table.column; SELECT * FROM ref_table,other_table WHERE ref_table.key_column_part1=other_table.column AND ref_table.key_column_part2=1;
例如:
mysql> drop index idx_t3_id on t3; Query OK, 1000 rows affected (0.03 sec) Records: 1000 Duplicates: 0 Warnings: 0 mysql> create index idx_t3_id on t3(id) ; Query OK, 1000 rows affected (0.04 sec) Records: 1000 Duplicates: 0 Warnings: 0
mysql> explain select * from t3,t4 where t3.id=t4.accountid;
+----+-------------+-------+------+-------------------+-----------+---------+----------------------+------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+-------------------+-----------+---------+----------------------+------+-------+
| 1 | SIMPLE | t4 | ALL | NULL | NULL | NULL | NULL | 1000 | |
| 1 | SIMPLE | t3 | ref | PRIMARY,idx_t3_id | idx_t3_id | 4 | dbatest.t4.accountid | 1 | |
+----+-------------+-------+------+-------------------+-----------+---------+----------------------+------+-------+
2 rows in set (0.00 sec)
(5). ref_or_null
該聯接類型好像ref,但是添加了MySQL可以專門搜索包括NULL值的行。在處理子查詢中常常運用該聯接類型的優化。
在上面的例子中,MySQL可以運用ref_or_null聯接來處置ref_tables:
SELECT * FROM ref_table
WHERE key_column=expr OR key_column IS NULL;
(6). index_merge
該聯接類型表示運用了索引兼並優化辦法。在這種狀況下,key列包括了運用的索引的清單,key_len包括了運用的索引的最長的關鍵元素。
例如:
mysql> explain select * from t4 where id=3952602 or accountid=31754306 ;
+----+-------------+-------+-------------+----------------------------+----------------------------+---------+------+------+------------------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+-------------+----------------------------+----------------------------+---------+------+------+------------------------------------------------------+
| 1 | SIMPLE | t4 | index_merge | idx_t4_id,idx_t4_accountid | idx_t4_id,idx_t4_accountid | 4,4 | NULL | 2 | Using union(idx_t4_id,idx_t4_accountid); Using where |
+----+-------------+-------+-------------+----------------------------+----------------------------+---------+------+------+------------------------------------------------------+
1 row in set (0.00 sec)
(7). unique_subquery
該類型交換了上面方式的IN子查詢的ref:
value IN (SELECT primary_key FROM single_table WHERE some_expr)
unique_subquery是一個索引查找函數,可以完全交換子查詢,效率更高。
(8).index_subquery
該聯接類型相似於unique_subquery。可以交換IN子查詢,但只合適下列方式的子查詢中的非獨一索引:
value IN (SELECT key_column FROM single_table WHERE some_expr)
(9).range
只檢索給定范圍的行,運用一個索引來選擇行。key列顯示運用了哪個索引。key_len包括所運用索引的最長關鍵元素。在該類型中ref列為NULL。
當運用=、<>、>、>=、<、<=、IS NULL、<=>、BETWEEN或許IN操作符,用常量比擬關鍵字列時,可以運用range
mysql> explain select * from t3 where id=3952602 or id=3952603 ;
+----+-------------+-------+-------+-------------------+-----------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+-------+-------------------+-----------+---------+------+------+-------------+
| 1 | SIMPLE | t3 | range | PRIMARY,idx_t3_id | idx_t3_id | 4 | NULL | 2 | Using where |
+----+-------------+-------+-------+-------------------+-----------+---------+------+------+-------------+
1 row in set (0.02 sec)
(10).index
該聯接類型與ALL相反,除了只要索引樹被掃描。這通常比ALL快,由於索引文件通常比數據文件小。
當查詢只運用作為單索引一局部的列時,MySQL可以運用該聯接類型。
(11). ALL
關於每個來自於先前的表的行組合,停止完好的表掃描。假如表是第一個沒標志const的表,這通常不好,並且通常在它狀況下很差。通常可以添加更多的索引而不要運用ALL,使得行能基於後面的表中的常數值或列值被檢索出。
5.possible_keys
possible_keys列指出MySQL能運用哪個索引在該表中找到行。留意,該列完全獨立於EXPLAIN輸入所示的表的次第。這意味著在possible_keys中的某些鍵實踐上不能按生成的表次第運用。
假如該列是NULL,則沒有相關的索引。在這種狀況下,可以經過反省WHERE子句看能否它援用某些列或合適索引的列來進步你的查詢功能。假如是這樣,發明一個適當的索引並且再次用EXPLAIN反省查詢
6. key
key列顯示MySQL實踐決議運用的鍵(索引)。假如沒有選擇索引,鍵是NULL。要想強迫MySQL運用或無視possible_keys列中的索引,在查詢中運用FORCE INDEX、USE INDEX或許IGNORE INDEX。
7.key_len
key_len列顯示MySQL決議運用的鍵長度。假如鍵是NULL,則長度為NULL。
運用的索引的長度。在不損失准確性的狀況下,長度越短越好
8. ref
ref列顯示運用哪個列或常數與key一同從表中選擇行。
9. rows
rows列顯示MySQL以為它執行查詢時必需反省的行數。
10. Extra
該列包括MySQL處理查詢的詳細信息,上面詳細.
(1).Distinct
一旦MYSQL找到了與行相結合婚配的行,就不再搜索了
(2).Not exists
MYSQL優化了LEFT JOIN,一旦它找到了婚配LEFT JOIN規范的行,
就不再搜索了
(3).Range checked for each
Record(index map:#)
沒有找到理想的索引,因而關於從後面表中來的每一個行組合,MYSQL反省運用哪個索引,並用它來從表中前往行。這是運用索引的最慢的銜接之一
(4).Using filesort
看到這個的時分,查詢就需求優化了。MYSQL需求停止額定的步驟來發現如何對前往的行排序。它依據銜接類型以及存儲排序鍵值和婚配條件的全部行的行指針來排序全部行
(5).Using index
列數據是從僅僅運用了索引中的信息而沒有讀取實踐的舉動的表前往的,這發作在對表的全部的懇求列都是同一個索引的局部的時分
(6).Using temporary
看到這個的時分,查詢需求優化了。這裡,MYSQL需求創立一個暫時表來存儲後果,這通常發作在對不同的列集停止ORDER BY上,而不是GROUP BY上
(7).Using where
運用了WHERE從句來限制哪些即將與下一張表婚配或許是前往給用戶。假如不想前往表中的全部行,並且銜接類型ALL或index,這就會發作,或許是查詢有問題
感激閱讀,希望能協助到大家,謝謝大家對本站的支持!