mysql explain 命令講解

來源:互聯網
上載者:User

標籤:

explian命令可以顯示select語句的執行計畫

explain的結果中每行對應select語句中的一個表,輸出結果中的順序是按照語句處理表的順序。 mysql使用嵌套迴圈來處理所有的join串連。 當使用了關鍵字extended後,explain可以查看到"show warnings"語句的內容,以及被過濾的列。

關鍵字"extented"和"partitions"不能一起使用,在5.6.5之後,這兩個關鍵字都不可以和"format"一起使用。

mysql> explain select * from emp ,dept where emp.deptno=dept.deptno;+----+-------------+-------+--------+---------------+------+---------+------+------+-------+| id | select_type | table | type   | possible_keys | key  | key_len | ref  | rows | Extra |+----+-------------+-------+--------+---------------+------+---------+------+------+-------+|  1 | SIMPLE      | emp   | system | NULL          | NULL | NULL    | NULL |    1 |       ||  1 | SIMPLE      | dept  | system | NULL          | NULL | NULL    | NULL |    1 |       |+----+-------------+-------+--------+---------------+------+---------+------+------+-------+2 rows in set (0.00 sec)mysql> explain select * from dept,emp where emp.deptno=dept.deptno;+----+-------------+-------+--------+---------------+------+---------+------+------+-------+| id | select_type | table | type   | possible_keys | key  | key_len | ref  | rows | Extra |+----+-------------+-------+--------+---------------+------+---------+------+------+-------+|  1 | SIMPLE      | dept  | system | NULL          | NULL | NULL    | NULL |    1 |       ||  1 | SIMPLE      | emp   | system | NULL          | NULL | NULL    | NULL |    1 |       |+----+-------------+-------+--------+---------------+------+---------+------+------+-------+2 rows in set (0.00 sec)mysql> explain select * from (select * from ( select * from emp where id=1) 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     | emp        | system | NULL          | NULL | NULL    | NULL |    1 |       |+----+-------------+------------+--------+---------------+------+---------+------+------+-------+3 rows in set (0.01 sec)mysql>

 

1.id (JSON name: select_id)
 select標識符,在查詢中該值是順序的數字。如果該行是其它行union的結果,該值可以為null。


2.select_type (JSON name: none)

select_type的取值列表

取值 json name 說明
simple   簡單的select查詢(沒有union、沒有子查詢)
primary   最外層的查詢
union   union中的第二個或後面的select語句
dependent union dependent (true) union中的第二個或後面的select語句,對外查詢有依賴
union result union_result union的結果集
subquery   子查詢中的第一個查詢
dependent subquery dependent (true) 子查詢中的第一個查詢,對外層查詢有依賴
derived   派生表的select(from子句的子查詢)
materialized materialized_from_subquery 物化的子查詢
uncacheable subquery cacheable (false) 結果不能被緩衝的子查詢,外層查詢需要使用的時候都要重新執行一次
uncacheable union cacheable (false) union中的第二個或者後面的不能被緩衝的子查詢

 

 

 

 

 

 

 

 

 

 

 

 

3.table (JSON name: table_name)  

<unionM,N>:表示是M行和N行結果的union;<derivedN>:表示派生自N行的結果;<subqueryN>: 引用N行的物化的子查詢

 

4.partitions (JSON name: partitions)
 查詢涉及的分區


5.type (JSON name: access_type)
 join的類型

 

6.possible_keys

possible_keys列指出MySQL可以使用的索引。注意,該列完全獨立於EXPLAIN輸出所示的表的次序。這意味著在possible_keys中的某些鍵實際上不能按產生的表次序使用。

如果該列是NULL,則沒有相關的索引。在這種情況下,可以通過檢查WHERE子句看是否它引用某些列或適合索引的列來提高你的查詢效能。如果是這樣,創造一個適當的索引並且再次用EXPLAIN檢查查詢

 

7.key (JSON name: key)

顯示MySQL實際決定使用的鍵(索引)。如果沒有選擇索引,鍵是NULL。

要想強制MySQL使用或不使用possible_keys列中的索引,在查詢中使用FORCE INDEX、USE INDEX或者IGNORE INDEX。

 

8.key_len (JSON name: key_length)

key_len列顯示MySQL決定使用的鍵長度。如果鍵是NULL,則長度為NULL。 使用的索引的長度。在不損失精確性的情況下,長度越短越好

 

9.ref (JSON name: ref)

ref列顯示使用哪個列或常數與key一起從表中選擇行。

 

10.rows (JSON name: rows)

rows列顯示MySQL認為它執行查詢時必須檢查的行數。

 

11.filtered (JSON name: filtered)

表的行數的過濾的百分比

 

12. Extra

包含MySQL執行查詢使用的其它資訊:

看到 Using filesort 和 Using temporary 的時候,查詢就需要最佳化了。

mysql explain 命令講解

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.