MySQL SQL Explain輸出學習--轉發
目錄
正文
MySQL的explain命令語句提供瞭如何執行SQL語句的資訊,解析SQL語句的執行計劃並展示,explain支援select、delete、insert、replace和update等語句,也支援對分割槽表的解析。通常explain用來獲取select語句的執行計劃,通過explain展示的資訊我們可以瞭解到表查詢的順序,表連線的方式等,並根據這些資訊判斷select執行效率,決定是否新增索引或改寫SQL語句優化表連線方式以提高執行效率。本文參考官方文件:
本文使用的MySQL版本為官方社群版 5.7.24
。
mysql root@localhost:(none)> select version();
+------------+
| version() |
+------------+
| 5.7.24-log |
+------------+
1 row in set
Time: 0.066s
回到頂部
主要用法
{ EXPLAIN | DESCRIBE } [EXTENDED | PARTITIONS | FORMAT=[TRADITIONAL | JSON]] SQL_STATEMENT
;
- EXPLAIN和DESCRIBE(可以簡寫成DESC)都可以用來檢視語句的執行計劃,但通常使用EXPLAIN較多;
- FORMAT選項可以指定執行計劃輸出資訊為JSON格式,而且包含一些更詳細的指標說明;
- EXTENDED和PARTITIONS選項可以輸出更詳細選項說明,語法上是為了相容低版本MySQL,未來會廢棄,預設使用EXPLAIN命令即可。
測試資料
本文基於MySQL官方示例資料庫employee:Example Databases進行解析說明,使用到的表如下:
-- employees:
mysql root@localhost:employees> show create table employees\G;
***************************[ 1. row ]***************************
Table | employees
Create Table | CREATE TABLE `employees` (
`emp_no` int(11) NOT NULL,
`birth_date` date NOT NULL,
`first_name` varchar(14) NOT NULL,
`last_name` varchar(16) NOT NULL,
`gender` enum('M','F') NOT NULL,
`hire_date` date NOT NULL,
PRIMARY KEY (`emp_no`),
KEY `idx_first_last` (`first_name`,`last_name`),
KEY `idx_birth_hire` (`birth_date`,`hire_date`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set
Time: 0.008s
-- dept_emp:
mysql root@localhost:employees> show create table dept_emp\G;
***************************[ 1. row ]***************************
Table | dept_emp
Create Table | CREATE TABLE `dept_emp` (
`emp_no` int(11) NOT NULL,
`dept_no` char(4) NOT NULL,
`from_date` date NOT NULL,
`to_date` date NOT NULL,
PRIMARY KEY (`emp_no`,`dept_no`),
KEY `dept_no` (`dept_no`),
CONSTRAINT `dept_emp_ibfk_1` FOREIGN KEY (`emp_no`) REFERENCES `employees` (`emp_no`) ON DELETE CASCADE,
CONSTRAINT `dept_emp_ibfk_2` FOREIGN KEY (`dept_no`) REFERENCES `departments` (`dept_no`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set
Time: 0.010s
-- departments:
mysql root@localhost:employees> show create table departments\G;
***************************[ 1. row ]***************************
Table | departments
Create Table | CREATE TABLE `departments` (
`dept_no` char(4) NOT NULL,
`dept_name` varchar(40) NOT NULL,
PRIMARY KEY (`dept_no`),
UNIQUE KEY `dept_name` (`dept_name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set
Time: 0.012s
回到頂部
輸出說明
mysql root@localhost:employees> explain select count(*) from employees;
+----+-------------+-----------+------------+-------+---------------+---------+---------+--------+--------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-----------+------------+-------+---------------+---------+---------+--------+--------+----------+-------------+
| 1 | SIMPLE | employees | <null> | index | <null> | PRIMARY | 4 | <null> | 299512 | 100.0 | Using index |
+----+-------------+-----------+------------+-------+---------------+---------+---------+--------+--------+----------+-------------+
1 row in set
Time: 0.026s
通過以上示例語句得出explain輸出有12個欄位,主要說明如下表:
欄位(Column) | JSON名稱(JSON Name) | 含義(Meaning) |
---|---|---|
id | select_id | 識別符號,語句涉及表的執行順序 |
select_type | None | 表查詢型別 |
table | table_name | 表名稱 |
partitions | partitions | 涉及表哪個分割槽 |
type | access_type | 表的查詢(連線)型別 |
possible_keys | possible_keys | 表可能使用到的索引 |
key | key | 表實際使用到的索引 |
key_len | key_length | 表實際使用索引的長度,單位:位元組 |
ref | ref | 表哪些欄位或者常量用於連線查詢索引上的值 |
rows | rows | 查詢預估返回表的行數 |
filtered | filtered | 表經過條件過濾之後與總數的百分比 |
Extra | None | 額外的說明資訊 |
id
id為select識別符號,語句在執行計劃當中的執行順序。id值的出現有如下幾種情況:
- id值全相同,則按由上到下順序執行;
- id值全不相同,則按id值大小,由大到小順序執行;
- id值部分相同,部分不相同,則同組id值大的優先執行(組內id值相同的順序執行)。
-- id全相同
mysql root@localhost:employees> explain select * from employees e,dept_emp d,departments de where e.emp_no = d.emp_no and de.dept_name = 'Human
Resources';
+----+-------------+-------+------------+-------+---------------+-----------+---------+--------------------+--------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+-------+---------------+-----------+---------+--------------------+--------+----------+-------------+
| 1 | SIMPLE | de | <null> | const | dept_name | dept_name | 122 | const | 1 | 100.0 | Using index |
| 1 | SIMPLE | e | <null> | ALL | PRIMARY | <null> | <null> | <null> | 299512 | 100.0 | <null> |
| 1 | SIMPLE | d | <null> | ref | PRIMARY | PRIMARY | 4 | employees.e.emp_no | 1 | 100.0 | <null> |
+----+-------------+-------+------------+-------+---------------+-----------+---------+--------------------+--------+----------+-------------+
3 rows in set
Time: 0.018s
-- id全不相同
mysql root@localhost:employees> explain select * from employees e where e.emp_no = (select d.emp_no from dept_emp d where d.dept_no = (select de.d
ept_no from departments de where de.dept_name = 'Development') and d.emp_no = 10023);
+----+-------------+-------+------------+-------+-----------------+-----------+---------+-------------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+-------+-----------------+-----------+---------+-------------+------+----------+-------------+
| 1 | PRIMARY | e | <null> | const | PRIMARY | PRIMARY | 4 | const | 1 | 100.0 | <null> |
| 2 | SUBQUERY | d | <null> | const | PRIMARY,dept_no | PRIMARY | 16 | const,const | 1 | 100.0 | Using index |
| 3 | SUBQUERY | de | <null> | const | dept_name | dept_name | 122 | const | 1 | 100.0 | Using index |
+----+-------------+-------+------------+-------+-----------------+-----------+---------+-------------+------+----------+-------------+
3 rows in set
Time: 0.027s
-- id部分相同,部分不相同
mysql root@localhost:employees> explain select * from^Iemployees e where^Ie.emp_no in (select d.emp_no from dept_emp d where d.dept_no = (select d
e.dept_no from departments de where de.dept_name = 'Human Resources'));
+----+-------------+-------+------------+--------+-----------------+-----------+---------+--------------------+-------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+--------+-----------------+-----------+---------+--------------------+-------+----------+-------------+
| 1 | PRIMARY | d | <null> | ref | PRIMARY,dept_no | dept_no | 12 | const | 33212 | 100.0 | Using index |
| 1 | PRIMARY | e | <null> | eq_ref | PRIMARY | PRIMARY | 4 | employees.d.emp_no | 1 | 100.0 | <null> |
| 3 | SUBQUERY | de | <null> | const | dept_name | dept_name | 122 | const | 1 | 100.0 | Using index |
+----+-------------+-------+------------+--------+-----------------+-----------+---------+--------------------+-------+----------+-------------+
3 rows in set
Time: 0.020s
回到頂部
select_type
select_type為表查詢的型別,根據官方文件總結幾種常見型別如下表:
select_type值(Value) | JSON名稱(JSON Name) | 含義(Meaning) |
---|---|---|
SIMPLE | None | 簡單查詢,不包含unino查詢或子查詢 |
PRIMARY | None | 位於最外層的查詢 |
UNION | None | 當出現union查詢時第二個或之後的查詢 |
DEPENDENT UNION | dependent(true) | 當出現union查詢時第二個或之後的查詢,取決於外部查詢 |
UNION RESULT | union_result | union查詢的結果 |
SUBQUERY | None | 子查詢當中第一個select查詢 |
DEPENDENT SUBQUERY | dependent(true) | 子查詢當中第一個select查詢,取決於外部的查詢 |
DERIVED | None | 派生表,from子句中出現的子查詢 |
- SIMPLE:最常見的查詢型別,通常情況下沒有子查詢、union查詢就是SIMPLE型別。
mysql root@localhost:employees> explain select * from employees where emp_no = 10001;
+----+-------------+-----------+------------+-------+---------------+---------+---------+-------+------+----------+--------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-----------+------------+-------+---------------+---------+---------+-------+------+----------+--------+
| 1 | SIMPLE | employees | <null> | const | PRIMARY | PRIMARY | 4 | const | 1 | 100.0 | <null> |
+----+-------------+-----------+------------+-------+---------------+---------+---------+-------+------+----------+--------+
1 row in set
Time: 0.019s
- PRIMARY和SUBQUERY:在含有子查詢的語句中會出現。
mysql root@localhost:employees> explain select * from dept_emp d where d.dept_no = (select de.dept_no from departments de where de.dept_name = 'De
velopment');
+----+-------------+-------+------------+-------+---------------+-----------+---------+-------+--------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+-------+---------------+-----------+---------+-------+--------+----------+-------------+
| 1 | PRIMARY | d | <null> | ref | dept_no | dept_no | 12 | const | 148054 | 100.0 | Using where |
| 2 | SUBQUERY | de | <null> | const | dept_name | dept_name | 122 | const | 1 | 100.0 | Using index |
+----+-------------+-------+------------+-------+---------------+-----------+---------+-------+--------+----------+-------------+
2 rows in set
Time: 0.021s
- UNION和UNION RESULT:在有union查詢的語句中出現。
mysql root@localhost:employees> explain select * from departments where dept_no = 'd005' union select * from departments where dept_no = 'd004';
+--------+--------------+-------------+------------+-------+---------------+---------+---------+--------+--------+----------+-----------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+--------+--------------+-------------+------------+-------+---------------+---------+---------+--------+--------+----------+-----------------+
| 1 | PRIMARY | departments | <null> | const | PRIMARY | PRIMARY | 12 | const | 1 | 100.0 | <null> |
| 2 | UNION | departments | <null> | const | PRIMARY | PRIMARY | 12 | const | 1 | 100.0 | <null> |
| <null> | UNION RESULT | <union1,2> | <null> | ALL | <null> | <null> | <null> | <null> | <null> | <null> | Using temporary |
+--------+--------------+-------------+------------+-------+---------------+---------+---------+--------+--------+----------+-----------------+
3 rows in set
Time: 0.020s
- DEPENDENT UNION和DEPENDENT SUBQUERY:當語句中子查詢和union查詢依賴外部查詢會出現。
mysql root@localhost:employees> explain select * from employees e where e.emp_no in (select d.emp_no from dept_emp d where d.from_date = '1986-06-
26' union select d.emp_no from dept_emp d where d.from_date = '1996-08-03');
+--------+--------------------+------------+------------+------+---------------+---------+---------+--------+--------+----------+-----------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+--------+--------------------+------------+------------+------+---------------+---------+---------+--------+--------+----------+-----------------+
| 1 | PRIMARY | e | <null> | ALL | <null> | <null> | <null> | <null> | 299512 | 100.0 | Using where |
| 2 | DEPENDENT SUBQUERY | d | <null> | ref | PRIMARY | PRIMARY | 4 | func | 1 | 10.0 | Using where |
| 3 | DEPENDENT UNION | d | <null> | ref | PRIMARY | PRIMARY | 4 | func | 1 | 10.0 | Using where |
| <null> | UNION RESULT | <union2,3> | <null> | ALL | <null> | <null> | <null> | <null> | <null> | <null> | Using temporary|
+--------+--------------------+------------+------------+------+---------------+---------+---------+--------+--------+----------+-----------------+
4 rows in set
Time: 0.022s
- DERIVED:當查詢涉及生成臨時表時出現。
mysql root@localhost:employees> explain select * from (select * from departments limit 5) de;
+----+-------------+-------------+------------+-------+---------------+-----------+---------+--------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------------+------------+-------+---------------+-----------+---------+--------+------+----------+-------------+
| 1 | PRIMARY | <derived2> | <null> | ALL | <null> | <null> | <null> | <null> | 5 | 100.0 | <null> |
| 2 | DERIVED | departments | <null> | index | <null> | dept_name | 122 | <null> | 9 | 100.0 | Using index |
+----+-------------+-------------+------------+-------+---------------+-----------+---------+--------+------+----------+-------------+
2 rows in set
Time: 0.012s
回到頂部
table
指執行計劃當中當前是從哪張表獲取資料,如果為表指定了別名,則顯示別名,如果沒有涉及對錶的資料讀取,則顯示NULL,還有如下幾種情形:
- <unionM,N>:資料來自union查詢的id為M和N的結果集;
- :資料來自派生表id為N的結果集;
- :資料來自子查詢id為N的結果集。
partitions
指執行計劃中當前從分割槽表哪個表分割槽獲取資料,如果不是分割槽表,則顯示為NULL。
-- 示例資料庫employees的分割槽表salaries
mysql root@localhost:employees> show create table salaries;
+----------+-----------------------------------------------------------------+
| Table | Create Table |
+----------+-----------------------------------------------------------------+
| salaries | CREATE TABLE `salaries` ( |
| | `emp_no` int(11) NOT NULL, |
| | `salary` int(11) NOT NULL, |
| | `from_date` date NOT NULL, |
| | `to_date` date NOT NULL, |
| | PRIMARY KEY (`emp_no`,`from_date`) |
| | ) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
| | /*!50500 PARTITION BY RANGE COLUMNS(from_date) |
| | (PARTITION p01 VALUES LESS THAN ('1985-12-31') ENGINE = InnoDB, |
| | PARTITION p02 VALUES LESS THAN ('1986-12-31') ENGINE = InnoDB, |
| | PARTITION p03 VALUES LESS THAN ('1987-12-31') ENGINE = InnoDB, |
| | PARTITION p04 VALUES LESS THAN ('1988-12-31') ENGINE = InnoDB, |
| | PARTITION p05 VALUES LESS THAN ('1989-12-31') ENGINE = InnoDB, |
| | PARTITION p06 VALUES LESS THAN ('1990-12-31') ENGINE = InnoDB, |
| | PARTITION p07 VALUES LESS THAN ('1991-12-31') ENGINE = InnoDB, |
| | PARTITION p08 VALUES LESS THAN ('1992-12-31') ENGINE = InnoDB, |
| | PARTITION p09 VALUES LESS THAN ('1993-12-31') ENGINE = InnoDB, |
| | PARTITION p10 VALUES LESS THAN ('1994-12-31') ENGINE = InnoDB, |
| | PARTITION p11 VALUES LESS THAN ('1995-12-31') ENGINE = InnoDB, |
| | PARTITION p12 VALUES LESS THAN ('1996-12-31') ENGINE = InnoDB, |
| | PARTITION p13 VALUES LESS THAN ('1997-12-31') ENGINE = InnoDB, |
| | PARTITION p14 VALUES LESS THAN ('1998-12-31') ENGINE = InnoDB, |
| | PARTITION p15 VALUES LESS THAN ('1999-12-31') ENGINE = InnoDB, |
| | PARTITION p16 VALUES LESS THAN ('2000-12-31') ENGINE = InnoDB, |
| | PARTITION p17 VALUES LESS THAN ('2001-12-31') ENGINE = InnoDB, |
| | PARTITION p18 VALUES LESS THAN ('2002-12-31') ENGINE = InnoDB, |
| | PARTITION p19 VALUES LESS THAN (MAXVALUE) ENGINE = InnoDB) */ |
+----------+-----------------------------------------------------------------+
1 row in set
Time: 0.018s
mysql root@localhost:employees> explain select * from salaries where from_date > '1985-12-31' and from_date < '1990-12-31';
+----+-------------+----------+---------------------+------+---------------+--------+---------+--------+--------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+----------+---------------------+------+---------------+--------+---------+--------+--------+----------+-------------+
| 1 | SIMPLE | salaries | p02,p03,p04,p05,p06 | ALL | <null> | <null> | <null> | <null> | 384341 | 11.11 | Using where |
+----+-------------+----------+---------------------+------+---------------+--------+---------+--------+--------+----------+-------------+
1 row in set
Time: 0.023s
回到頂部
type
type應該被認為是解讀執行計劃當中最重要的部分,根據type顯示的內容可以判斷語句總體的查詢效率。主要有以下幾種型別:
- system:表只有一行(系統表),是const的一種特殊情況。
-- 測試表departments_1生成:
mysql root@localhost:employees> create table departments_1 as select * from departments where dept_no='d005';
Query OK, 1 row affected
Time: 0.107s
mysql root@localhost:employees> alter table departments_1 add primary key(dept_no);
Query OK, 0 rows affected
mysql root@localhost:employees> create index idx_dept_name on departments_1(dept_name);
Query OK, 0 rows affected
mysql root@localhost:employees> show create table departments_1\G;
***************************[ 1. row ]***************************
Table | departments_1
Create Table | CREATE TABLE `departments_1` (
`dept_no` char(4) NOT NULL,
`dept_name` varchar(40) DEFAULT NULL,
PRIMARY KEY (`dept_no`),
KEY `idx_dept_name` (`dept_name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set
Time: 0.010s
-- 系統表:
mysql root@localhost:employees> explain select * from mysql.proxies_priv;
+----+-------------+--------------+------------+--------+---------------+--------+---------+--------+------+----------+--------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+--------------+------------+--------+---------------+--------+---------+--------+------+----------+--------+
| 1 | SIMPLE | proxies_priv | <null> | system | <null> | <null> | <null> | <null> | 1 | 100.0 | <null> |
+----+-------------+--------------+------------+--------+---------------+--------+---------+--------+------+----------+--------+
1 row in set
Time: 0.023s
-- 普通表:
mysql root@localhost:employees> explain select * from (select * from departments_1 where dept_no = 'd005' limit 1) de;
+----+-------------+---------------+------------+--------+---------------+---------+---------+--------+------+----------+--------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+---------------+------------+--------+---------------+---------+---------+--------+------+----------+--------+
| 1 | PRIMARY | <derived2> | <null> | system | <null> | <null> | <null> | <null> | 1 | 100.0 | <null> |
| 2 | DERIVED | departments_1 | <null> | const | PRIMARY | PRIMARY | 12 | const | 1 | 100.0 | <null> |
+----+-------------+---------------+------------+--------+---------------+---------+---------+--------+------+----------+--------+
2 rows in set
Time: 0.015s
- const:對於主鍵或者唯一索引鍵的等值查詢,只返回一行資料。
mysql root@localhost:employees> explain select * from departments_1 where dept_no = 'd005';
+----+-------------+---------------+------------+-------+---------------+---------+---------+-------+------+----------+--------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+---------------+------------+-------+---------------+---------+---------+-------+------+----------+--------+
| 1 | SIMPLE | departments_1 | <null> | const | PRIMARY | PRIMARY | 12 | const | 1 | 100.0 | <null> |
+----+-------------+---------------+------------+-------+---------------+---------+---------+-------+------+----------+--------+
1 row in set
Time: 0.018s
- eq_ref:對於前表的每一行資料,都只能匹配當前表唯一一行資料。除了system與const之外這是最好的一種連線查詢型別,主鍵或者是非空唯一索引的所有部分都可以在連線時被使用,通常使用的是'='操作符,比較值可以是一個常量,也可以是一個在該表之前讀取該表的欄位表示式。
explain select * from departments d,departments_1 d1 where d.dept_no = d1.dept_no;
+----+-------------+-------+------------+--------+---------------+---------------+---------+----------------------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+--------+---------------+---------------+---------+----------------------+------+----------+-------------+
| 1 | SIMPLE | d1 | <null> | index | PRIMARY | idx_dept_name | 123 | <null> | 1 | 100.0 | Using index|
| 1 | SIMPLE | d | <null> | eq_ref | PRIMARY | PRIMARY | 12 | employees.d1.dept_no | 1 | 100.0 | <null> |
+----+-------------+-------+------------+--------+---------------+---------------+---------+----------------------+------+----------+-------------+
2 rows in set
Time: 0.037s
- ref:對於前表的每一行資料,都從當前表讀取所有匹配索引值的行。與eq_ref相比,連線查詢欄位不是主鍵或者唯一索引,又或者是複合索引的部分左字首,如果連線查詢匹配的是少量幾行資料,ref是個不同錯的選擇,通常使用的運算子是'='、'<='或者'>='等。
mysql root@localhost:employees> explain select * from dept_emp where dept_no ='d005';
+----+-------------+----------+------------+------+---------------+---------+---------+-------+--------+----------+--------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+----------+------------+------+---------------+---------+---------+-------+--------+----------+--------+
| 1 | SIMPLE | dept_emp | <null> | ref | dept_no | dept_no | 12 | const | 148054 | 100.0 | <null> |
+----+-------------+----------+------------+------+---------------+---------+---------+-------+--------+----------+--------+
1 row in set
Time: 0.059s
mysql root@localhost:employees> explain select * from dept_emp d,departments_1 d1 where d.dept_no = d1.dept_no;
+----+-------------+-------+------------+------+---------------+---------+---------+----------------------+-------+----------+--------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+------+---------------+---------+---------+----------------------+-------+----------+--------+
| 1 | SIMPLE | d1 | <null> | ALL | <null> | <null> | <null> | <null> | 1 | 100.0 | <null> |
| 1 | SIMPLE | d | <null> | ref | dept_no | dept_no | 12 | employees.d1.dept_no | 41392 | 100.0 | <null> |
+----+-------------+-------+------------+------+---------------+---------+---------+----------------------+-------+----------+--------+
2 rows in set
Time: 0.012s
- ref_or_null:同ref型別,但是包含了對NULL值的搜尋。
mysql root@localhost:employees> explain select dept_name from departments_1 where dept_name = 'd005' or dept_name is null;
+----+-------------+---------------+------------+-------------+---------------+---------------+---------+-------+------+----------+--------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+---------------+------------+-------------+---------------+---------------+---------+-------+------+----------+--------------------------+
| 1 | SIMPLE | departments_1 | <null> | ref_or_null | idx_dept_name | idx_dept_name | 123 | const | 2 | 100.0 | Using where; Using index |
+----+-------------+---------------+------------+-------------+---------------+---------------+---------+-------+------+----------+--------------------------+
1 row in set
Time: 0.011s
- index_merge:使用了索引合併優化進行查詢。如果查詢指定條件涉及對多個索引的使用時,會將多個索引合併操作。
mysql root@localhost:employees> explain select * from dept_emp where emp_no = 10001 or dept_no = (select dept_no from departments_1);
+----+-------------+---------------+------------+-------------+-----------------+-----------------+---------+--------+--------+----------+-------------------------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+---------------+------------+-------------+-----------------+-----------------+---------+--------+--------+----------+-------------------------------------------+
| 1 | PRIMARY | dept_emp | <null> | index_merge | PRIMARY,dept_no | PRIMARY,dept_no | 4,12 | <null> | 148055 | 100.0 | Using union(PRIMARY,dept_no); Using where |
| 2 | SUBQUERY | departments_1 | <null> | index | <null> | idx_dept_name | 123 | <null> | 1 | 100.0 | Using index |
+----+-------------+---------------+------------+-------------+-----------------+-----------------+---------+--------+--------+----------+-------------------------------------------+
2 rows in set
Time: 0.014s
- range:使用索引掃描條件指定範圍內的資料。常用的操作符有'>'、'<'、'is null'、'between'、'in'和'like'等。
mysql root@localhost:employees> explain select de.* from dept_emp de,departments_1 d where de.dept_no = d.dept_no and de.emp_no < 10010;
+----+-------------+-------+------------+-------+-----------------+---------------+---------+--------+------+----------+----------------------------------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+-------+-----------------+---------------+---------+--------+------+----------+----------------------------------------------------+
| 1 | SIMPLE | d | <null> | index | PRIMARY | idx_dept_name | 123 | <null> | 1 | 100.0 | Using index |
| 1 | SIMPLE | de | <null> | range | PRIMARY,dept_no | PRIMARY | 4 | <null> | 9 | 12.5 | Using where; Using join buffer (Block Nested Loop) |
+----+-------------+-------+------------+-------+-----------------+---------------+---------+--------+------+----------+----------------------------------------------------+
2 rows in set
Time: 0.019s
- index:使用索引全掃描。類似於全表掃描,只是掃描物件是索引,出現於以下兩種情況:
- 如果索引是覆蓋索引,即索引包含查詢所需要的所有表資料,就只掃描索引,並且在Extra中出現Using index。通常情況下掃描索引比打描表要更快,因為索引一般比表來的小;
- 全表掃描採用索引的順序來讀取資料,本質上還是全表掃描,並且在Extra中不會出現Using index,避免再進行排序消耗效能,因為索引本身就是排序好的。
mysql root@localhost:employees> explain select dept_name from departments_1;
+----+-------------+---------------+------------+-------+---------------+---------------+---------+--------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+---------------+------------+-------+---------------+---------------+---------+--------+------+----------+-------------+
| 1 | SIMPLE | departments_1 | <null> | index | <null> | idx_dept_name | 123 | <null> | 1 | 100.0 | Using index |
+----+-------------+---------------+------------+-------+---------------+---------------+---------+--------+------+----------+-------------+
1 row in set
Time: 0.020s
- all:使用全表掃描。
mysql root@localhost:employees> drop index idx_dept_name on departments_1;
Query OK, 0 rows affected
Time: 0.052s
mysql root@localhost:employees> explain select * from departments_1;
+----+-------------+---------------+------------+------+---------------+--------+---------+--------+------+----------+--------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+---------------+------------+------+---------------+--------+---------+--------+------+----------+--------+
| 1 | SIMPLE | departments_1 | <null> | ALL | <null> | <null> | <null> | <null> | 1 | 100.0 | <null> |
+----+-------------+---------------+------------+------+---------------+--------+---------+--------+------+----------+--------+
1 row in set
Time: 0.018s
通過以上各種主要型別的分析,可以總結出各個型別效能排序(從左到右效能從高到低):
回到頂部system > const > eq_ref > ref > range > index > all
possible_keys
顯示了MySQL在查詢當前表中資料的時候可能使用到的索引,如果該欄位值為NULL,則表明沒有相關索引可用。
回到頂部key
顯示了MySQL在實際查詢資料時決定使用的索引,如果該欄位值為NULL,則表明沒有使用索引。
回到頂部key_len
顯示了MySQL實際使用索引的鍵大小,單位位元組。可以通過key_len的大小判斷評估複合索引使用了哪些部分,如果key欄位值為NULL,則key_len的值也為NULL。
幾種常見欄位型別索引長度大小如下,假設字元編碼為UTF8:
- 欄位屬性是否允許NULL,如果允許NULL,則需要額外增加一個位元組;
- 字元型:
- char(n):3n個位元組
- varchar(n):3n+2個位元組
- 數值型:
- tinyint:1個位元組
- int:4個位元組
- bigint:8個位元組
- 時間型:
- date:3個位元組
- datetime:5個位元組+秒精度位元組
- timestamp:4個位元組+秒精度位元組
- 秒精度位元組(最大6位):
- 1~2位:1個位元組
- 3~4位:2個位元組
- 5~6位:3個位元組
ref
顯示哪些常量或者欄位被用於查詢索引列鍵值,以獲取表中資料行。
- 如果是常量等值查詢,則顯示為const;
- 如果是連線查詢,則被驅動表的該欄位會顯示驅動表的所關聯欄位;
- 如果條件當中使用函式表示式,或者值導致條件欄位發生隱式轉換,這裡顯示為func。
mysql root@localhost:employees> explain select * from departments d,departments_1 d1 where d.dept_no = d1.dept_no;
+----+-------------+-------+------------+--------+---------------+---------+---------+----------------------+------+----------+--------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+--------+---------------+---------+---------+----------------------+------+----------+--------+
| 1 | SIMPLE | d1 | <null> | ALL | PRIMARY | <null> | <null> | <null> | 1 | 100.0 | <null> |
| 1 | SIMPLE | d | <null> | eq_ref | PRIMARY | PRIMARY | 12 | employees.d1.dept_no | 1 | 100.0 | <null> |
+----+-------------+-------+------------+--------+---------------+---------+---------+----------------------+------+----------+--------+
2 rows in set
Time: 0.038s
回到頂部
rows
顯示預估需要查詢的行數。對InnoDB表來說這是個預估值,並非是個準確值。
回到頂部filtered
顯示按表條件過濾的錶行的估計百分比。
回到頂部Extra
顯示查詢時的額外資訊。常見的有如下幾種:
- Using index
僅查詢索引樹就可以獲取到所需要的資料行,而不需要讀取表中實際的資料行。通常適用於select欄位就是查詢使用索引的一部分,即使用了覆蓋索引。
mysql root@localhost:employees> explain select dept_name from departments_1;
+----+-------------+---------------+------------+-------+---------------+---------------+---------+--------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+---------------+------------+-------+---------------+---------------+---------+--------+------+----------+-------------+
| 1 | SIMPLE | departments_1 | <null> | index | <null> | idx_dept_name | 123 | <null> | 1 | 100.0 | Using index |
+----+-------------+---------------+------------+-------+---------------+---------------+---------+--------+------+----------+-------------+
1 row in set
Time: 0.015s
- Using index condition
顯示採用了Index Condition Pushdown (ICP)特性通過索引去表中獲取資料。關於ICP特性可以參考官方文件:Index Condition Pushdown Optimization。簡單說法如下:
- 如果開啟ICP特性,部分where條件部分可以下推到儲存引擎通過索引進行過濾,ICP可以減少儲存引擎訪問基表的次數;
- 如果沒有開啟ICP特性,則儲存引擎根據索引需要直接訪問基表獲取資料並返回給server層進行where條件的過濾。
-- employees表建立複合索引idx_birth_hire
mysql root@localhost:employees> create index idx_birth_hire on employees(birth_date,hire_date);
Query OK, 0 rows affected
Time: 0.768s
mysql root@localhost:employees> explain select * from employees where birth_date = '1960-01-01' and hire_date > '1980-01-01';
+----+-------------+-----------+------------+-------+----------------+----------------+---------+--------+------+----------+-----------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-----------+------------+-------+----------------+----------------+---------+--------+------+----------+-----------------------+
| 1 | SIMPLE | employees | <null> | range | idx_birth_hire | idx_birth_hire | 6 | <null> | 63 | 100.0 | Using index condition |
+----+-------------+-----------+------------+-------+----------------+----------------+---------+--------+------+----------+-----------------------+
1 row in set
Time: 0.016s
- Using index for group-by
跟Using index訪問表的方式類似,顯示MySQL通過索引就可以完成對GROUP BY或DISTINCT欄位的查詢,而無需再訪問表中的資料。
mysql root@localhost:employees> explain select distinct dept_no from dept_emp;
+----+-------------+----------+------------+-------+-----------------+---------+---------+--------+------+----------+--------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+----------+------------+-------+-----------------+---------+---------+--------+------+----------+--------------------------+
| 1 | SIMPLE | dept_emp | <null> | range | PRIMARY,dept_no | dept_no | 12 | <null> | 9 | 100.0 | Using index for group-by |
+----+-------------+----------+------------+-------+-----------------+---------+---------+--------+------+----------+--------------------------+
1 row in set
Time: 0.020s
- Using where
顯示MySQL通過索引條件定位之後還需要返回表中獲得所需要的資料。
mysql root@localhost:employees> explain select * from employees where birth_date < '1970-01-01';
+----+-------------+-----------+------------+------+----------------+--------+---------+--------+--------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-----------+------------+------+----------------+--------+---------+--------+--------+----------+-------------+
| 1 | SIMPLE | employees | <null> | ALL | idx_birth_hire | <null> | <null> | <null> | 299512 | 50.0 | Using where |
+----+-------------+-----------+------------+------+----------------+--------+---------+--------+--------+----------+-------------+
1 row in set
Time: 0.016s
- Impossible WHERE
where子句的條件永遠都不可能為真。
mysql root@localhost:employees> explain select * from employees where 1 = 0;
+----+-------------+--------+------------+--------+---------------+--------+---------+--------+--------+----------+------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+--------+------------+--------+---------------+--------+---------+--------+--------+----------+------------------+
| 1 | SIMPLE | <null> | <null> | <null> | <null> | <null> | <null> | <null> | <null> | <null> | Impossible WHERE |
+----+-------------+--------+------------+--------+---------------+--------+---------+--------+--------+----------+------------------+
1 row in set
Time: 0.015s
- Using join buffer (Block Nested Loop), Using join buffer (Batched Key Access)
在表聯接過程當中,將先前表的部分資料讀取到join buffer緩衝區中,然後從緩衝區中讀取資料與當前表進行連線。主要有兩種演算法:Block Nested Loop和Batched Key Access,關於這兩種演算法說明可以參考官方文件:Block Nested-Loop and Batched Key Access Joins,也可以參考另一篇博文說明:MySQL 查詢優化之 Block Nested-Loop 與 Batched Key Access Joins。
-- Block Nested Loop
mysql root@localhost:employees> explain select * from employees e,dept_emp d where e.emp_no > 10001 and e.emp_no <> d.emp_no;
+----+-------------+-------+------------+-------+---------------+---------+---------+--------+--------+----------+----------------------------------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+-------+---------------+---------+---------+--------+--------+----------+----------------------------------------------------+
| 1 | SIMPLE | e | <null> | range | PRIMARY | PRIMARY | 4 | <null> | 149756 | 100.0 | Using where |
| 1 | SIMPLE | d | <null> | ALL | <null> | <null> | <null> | <null> | 331143 | 90.0 | Using where; Using join buffer(Block Nested Loop) |
+----+-------------+-------+------------+-------+---------------+---------+---------+--------+--------+----------+----------------------------------------------------+
2 rows in set
Time: 0.020s
-- Batched Key Access
mysql root@localhost:employees> explain SELECT /*+ bka(a)*/ a.gender, b.dept_no FROM employees a, dept_emp b WHERE a.birth_date = b.from_date;
+----+-------------+-------+------------+------+----------------+----------------+---------+-----------------------+--------+----------+----------------------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+------+----------------+----------------+---------+-----------------------+--------+----------+----------------------------------------+
| 1 | SIMPLE | b | <null> | ALL | <null> | <null> | <null> | <null> | 331143 | 100.0 | <null> |
| 1 | SIMPLE | a | <null> | ref | idx_birth_hire | idx_birth_hire | 3 | employees.b.from_date | 63 | 100.0 | Using join buffer (Batched Key Access) |
+----+-------------+-------+------------+------+----------------+----------------+---------+-----------------------+--------+----------+----------------------------------------+
2 rows in set
Time: 0.014s
- Using MRR
讀取資料採用多範圍讀(Multi-Range Read)的優化策略。關於MRR特性也可以參考官方文件:Multi-Range Read Optimization
mysql root@localhost:employees> set optimizer_switch='mrr=on,mrr_cost_based=off';
Query OK, 0 rows affected
Time: 0.001s
mysql root@localhost:employees> explain select * from employees where birth_date = '1970-01-01' and hire_date > '1990-01-01';
+----+-------------+-----------+------------+-------+----------------+----------------+---------+--------+------+----------+----------------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-----------+------------+-------+----------------+----------------+---------+--------+------+----------+----------------------------------+
| 1 | SIMPLE | employees | <null> | range | idx_birth_hire | idx_birth_hire | 6 | <null> | 1 | 100.0 | Using index condition; Using MRR |
+----+-------------+-----------+------------+-------+----------------+----------------+---------+--------+------+----------+----------------------------------+
1 row in set
Time: 0.014s
- Range checked for each record (index map: N)
MySQL在獲取資料時發現在沒有索引可用,但當獲取部分先前表字段值時發現可以採用當前表某些索引來獲取資料。index map展示的是一個掩碼值,如index map:0x19,對應二進位制值為11001,表示當前表索引編號為1、4和5號索引可能被用來獲取資料,索引編號通過SHOW INDEX語句獲得。
mysql root@localhost:employees> explain select * from employees e,dept_emp d where e.emp_no > d.emp_no;
+----+-------------+-------+------------+------+---------------+--------+---------+--------+--------+----------+------------------------------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+------+---------------+--------+---------+--------+--------+----------+------------------------------------------------+
| 1 | SIMPLE | d | <null> | ALL | PRIMARY | <null> | <null> | <null> | 331143 | 100.0 | <null> |
| 1 | SIMPLE | e | <null> | ALL | PRIMARY | <null> | <null> | <null> | 299512 | 33.33 | Range checked for each record (index map: 0x1) |
+----+-------------+-------+------------+------+---------------+--------+---------+--------+--------+----------+------------------------------------------------+
2 rows in set
Time: 0.038s
- Select tables optimized away
MySQL優化器能夠確定以下兩點:
- 最多隻有一行記錄被返回;
- 為了獲取這一行資料,有一定的結果集需要獲取。
當語句在優化器階段過程中可以獲取查詢結果(如獲取行數,只需要讀取相應索引資料),而無需再返回表中查詢資料,可能會出現Select tables optimized away。例如針對MyISAM引擎的表,使用select count(*)獲取表的總行數,而且又沒有where子句或者條件總是為真,也沒有GROUP BY子句時,其實就包含了以上的條件且隱式含有GROUP BY分組的效果。
-- 建立MyISAM引擎的employees表
mysql root@localhost:employees> create table employees_myisam like employees;
Query OK, 0 rows affected
Time: 0.040s
mysql root@localhost:employees> insert into employees_myisam select * from employees;
Query OK, 300024 rows affected
Time: 5.023s
mysql root@localhost:employees> alter table employees_myisam engine=MyISAM;
Query OK, 300024 rows affected
Time: 1.515s
-- 獲取執行count(*)查詢行數執行計劃
mysql root@localhost:employees> explain select count(*) from employees_myisam;
+----+-------------+--------+------------+--------+---------------+--------+---------+--------+--------+----------+------------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+--------+------------+--------+---------------+--------+---------+--------+--------+----------+------------------------------+
| 1 | SIMPLE | <null> | <null> | <null> | <null> | <null> | <null> | <null> | <null> | <null> | Select tables optimized away |
+----+-------------+--------+------------+--------+---------------+--------+---------+--------+--------+----------+------------------------------+
1 row in set
Time: 0.024s
- Using temporary
MySQL需要建立臨時表來存放查詢結果集。通常發生在有GROUP BY或ORDER BY子句的語句當中。
mysql root@localhost:employees> explain select hire_date from employees group by hire_date;
+----+-------------+-----------+------------+-------+----------------+----------------+---------+--------+--------+----------+----------------------------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra
|
+----+-------------+-----------+------------+-------+----------------+----------------+---------+--------+--------+----------+----------------------------------------------+
| 1 | SIMPLE | employees | <null> | index | idx_birth_hire | idx_birth_hire | 6 | <null> | 299512 | 100.0 | Using index; Using temporary; Using filesort |
+----+-------------+-----------+------------+-------+----------------+----------------+---------+--------+--------+----------+----------------------------------------------+
1 row in set
Time: 0.018s
- Using filesort
MySQL需要對獲取的資料進行額外的一次排序操作,無法通過索引的排序完成。通常發生在有ORDER BY子句的語句當中。
mysql root@localhost:employees> explain select * from employees order by hire_date;
+----+-------------+-----------+------------+------+---------------+--------+---------+--------+--------+----------+----------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-----------+------------+------+---------------+--------+---------+--------+--------+----------+----------------+
| 1 | SIMPLE | employees | <null> | ALL | <null> | <null> | <null> | <null> | 299512 | 100.0 | Using filesort |
+----+-------------+-----------+------------+------+---------------+--------+---------+--------+--------+----------+----------------+
1 row in set
Time: 0.015s
回到頂部
總結
以上內容總結了MySQL獲取執行計劃explain命令執行時輸出的主要欄位說明,還有許多未仔細說明的引數和選項,以後還需多多實踐總結。可以看出explain命令輸出內容當中比較重要的是:
- type:展示了表的查詢/連線型別,體現查詢效率;
- key/key_len:實際使用了什麼索引,使用了哪些部分索引;
- Extra:對執行計劃步驟額外的說明,採用了哪些查詢特性。
參考
https://dev.mysql.com/doc/refman/5.7/en/explain-output.html
https://dev.mysql.com/doc/index-other.html
https://blog.csdn.net/u012410733/article/details/66472157
https://blog.csdn.net/wanbin6470398/article/details/82425620
https://blog.csdn.net/tianya9704/article/details/80067860
☆〖本人水平有限,文中如有錯誤還請留言批評指正!〗☆