MySQL - Explain深度剖析
文章目錄
- 生猛干貨
- 官方文檔
- Explain介紹
- 測(cè)試數(shù)據(jù)
- explain 使用
- explain重要列說(shuō)明
- id
- select_type
- simple
- primary
- subquery
- derived
- union
- table
- type
- NULL
- const, system
- eq_ref
- ref
- range
- index
- ALL
- possible_keys
- key
- key_len
- key_len計(jì)算規(guī)則
- ref
- rows
- Extra
- Using index
- Using where
- Using index condition
- Using temporary
- Using filesort
- Select tables optimized away
- 搞定MySQL實(shí)戰(zhàn)
生猛干貨
帶你搞定MySQL實(shí)戰(zhàn),輕松對(duì)應(yīng)海量業(yè)務(wù)處理及高并發(fā)需求,從容應(yīng)對(duì)大場(chǎng)面試
官方文檔
https://dev.mysql.com/doc/refman/5.7/en/explain-output.html
Explain介紹
使用EXPLAIN關(guān)鍵字可以模擬優(yōu)化器執(zhí)行SQL語(yǔ)句,分查詢語(yǔ)句或是結(jié)構(gòu)的性能瓶頸
在 select 語(yǔ)句之前增加 explain 關(guān)鍵字,MySQL 會(huì)在查詢上設(shè)置一個(gè)標(biāo)記,執(zhí)行查詢會(huì)返回執(zhí)行計(jì)劃的信息,而不是執(zhí)行這條SQL。
如果 from 中包含子查詢,仍會(huì)執(zhí)行該子查詢,將結(jié)果放入臨時(shí)表中 。
測(cè)試數(shù)據(jù)
DB Version
mysql> select version(); +------------+ | version() | +------------+ | 5.7.29-log | +------------+ 1 row in set (0.00 sec)mysql> DROP TABLE IF EXISTS `actor`;CREATE TABLE `actor` (`id` int(11) NOT NULL,`name` varchar(45) DEFAULT NULL,`update_time` datetime DEFAULT NULL,PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;INSERT INTO `actor` (`id`, `name`, `update_time`) VALUES (1,'a','2017-12-22 15:27:18'), (2,'b','2017-12-22 15:27:18'), (3,'c','2017-12-22 15:27:18');###############################DROP TABLE IF EXISTS `film`;CREATE TABLE `film` (`id` int(11) NOT NULL AUTO_INCREMENT,`name` varchar(10) DEFAULT NULL,PRIMARY KEY (`id`),KEY `idx_name` (`name`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;INSERT INTO `film` (`id`, `name`) VALUES (3,'film0'),(1,'film1'),(2,'film2');###############################DROP TABLE IF EXISTS `film_actor`;CREATE TABLE `film_actor` (`id` int(11) NOT NULL,`film_id` int(11) NOT NULL,`actor_id` int(11) NOT NULL,`remark` varchar(255) DEFAULT NULL,PRIMARY KEY (`id`),KEY `idx_film_actor_id` (`film_id`,`actor_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;INSERT INTO `film_actor` (`id`, `film_id`, `actor_id`) VALUES (1,1,1),(2,1,2),(3,2,1);explain 使用
explain 兩個(gè)擴(kuò)展的使用
explain extended: 提供: 額外一些查詢優(yōu)化的信息 (‘EXTENDED’ is deprecated and will be removed in a future release.)
mysql> explain extended select * from film where id=1; +----+-------------+-------+------------+-------+---------------+---------+---------+-------+------+----------+-------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+-------+------------+-------+---------------+---------+---------+-------+------+----------+-------+ | 1 | SIMPLE | film | NULL | const | PRIMARY | PRIMARY | 4 | const | 1 | 100.00 | NULL | +----+-------------+-------+------------+-------+---------------+---------+---------+-------+------+----------+-------+ 1 row in set, 2 warnings (0.00 sec)# 2 warnings# 可以通過(guò) show warnings 命令查看mysql> show warnings; +---------+------+----------------------------------------------------------------------------------+ | Level | Code | Message | +---------+------+----------------------------------------------------------------------------------+ | Warning | 1681 | 'EXTENDED' is deprecated and will be removed in a future release. | | Note | 1003 | /* select#1 */ select '1' AS `id`,'film1' AS `name` from `dbtest`.`film` where 1 | +---------+------+----------------------------------------------------------------------------------+ 2 rows in set (0.00 sec)mysql>filtered 列: 百分比,計(jì)算公式 rows * filtered/100 可以估算出將要和 explain 中前一個(gè)表進(jìn)行連接的行數(shù)(前一個(gè)表指 explain 中的id值比當(dāng)前表id值小的表) , 供參考
第二個(gè) ‘PARTITIONS’ is deprecated and will be removed in a future
mysql> explain partitions select * from film where id=1; +----+-------------+-------+------------+-------+---------------+---------+---------+-------+------+----------+-------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+-------+------------+-------+---------------+---------+---------+-------+------+----------+-------+ | 1 | SIMPLE | film | NULL | const | PRIMARY | PRIMARY | 4 | const | 1 | 100.00 | NULL | +----+-------------+-------+------------+-------+---------------+---------+---------+-------+------+----------+-------+ 1 row in set, 2 warnings (0.00 sec)mysql> show warnings; +---------+------+----------------------------------------------------------------------------------+ | Level | Code | Message | +---------+------+----------------------------------------------------------------------------------+ | Warning | 1681 | 'PARTITIONS' is deprecated and will be removed in a future release. | | Note | 1003 | /* select#1 */ select '1' AS `id`,'film1' AS `name` from `dbtest`.`film` where 1 | +---------+------+----------------------------------------------------------------------------------+ 2 rows in set (0.00 sec)mysql>所以只使用explain就足夠了 。
explain重要列說(shuō)明
mysql> explain select * from film_actor a where a.actor_id = (select id from actor where name = 'a'); +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+ | 1 | PRIMARY | a | NULL | ALL | NULL | NULL | NULL | NULL | 3 | 33.33 | Using where | | 2 | SUBQUERY | actor | NULL | ALL | NULL | NULL | NULL | NULL | 3 | 33.33 | Using where | +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+ 2 rows in set, 1 warning (0.00 sec)id
id列的編號(hào)是 select 的序列號(hào),有幾個(gè) select 就有幾個(gè)id,并且id的順序是按 select 出現(xiàn)的順序增長(zhǎng)的。
執(zhí)行順序:
id越大執(zhí)行優(yōu)先級(jí)越高,id相同則從上往下執(zhí)行,id為NULL最后執(zhí)行
select_type
表示的對(duì)應(yīng)行是簡(jiǎn)單還是復(fù)雜的查詢
simple
簡(jiǎn)單查詢,查詢不包含子查詢和union
mysql> explain select * from film where id=1; +----+-------------+-------+------------+-------+---------------+---------+---------+-------+------+----------+-------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+-------+------------+-------+---------------+---------+---------+-------+------+----------+-------+ | 1 | SIMPLE | film | NULL | const | PRIMARY | PRIMARY | 4 | const | 1 | 100.00 | NULL | +----+-------------+-------+------------+-------+---------------+---------+---------+-------+------+----------+-------+ 1 row in set, 1 warning (0.00 sec)mysql>primary
復(fù)雜查詢中最外層的 select
mysql> explain select * from film_actor a where a.actor_id = (select id from actor where name = 'a'); +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+ | 1 | PRIMARY | a | NULL | ALL | NULL | NULL | NULL | NULL | 3 | 33.33 | Using where | | 2 | SUBQUERY | actor | NULL | ALL | NULL | NULL | NULL | NULL | 3 | 33.33 | Using where | +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+ 2 rows in set, 1 warning (0.00 sec)subquery
包含在 select 中的子查詢(不在 from 子句中)
mysql> explain select * from film_actor a where a.actor_id = (select id from actor where name = 'a'); +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+ | 1 | PRIMARY | a | NULL | ALL | NULL | NULL | NULL | NULL | 3 | 33.33 | Using where | | 2 | SUBQUERY | actor | NULL | ALL | NULL | NULL | NULL | NULL | 3 | 33.33 | Using where | +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+ 2 rows in set, 1 warning (0.00 sec)derived
包含在 from 子句中的子查詢。
MySQL會(huì)將結(jié)果存放在一個(gè)臨時(shí)表中,也稱為派生表
derived: 衍生的
mysql> set session optimizer_switch='derived_merge=off'; #關(guān)閉mysql5.7新特性對(duì)衍生表的合并優(yōu)化 Query OK, 0 rows affected (0.00 sec)mysql> explain select (select 1 from actor where id = 1 ) from (select * from film where id =1 ) t ; +----+-------------+------------+------------+--------+---------------+---------+---------+-------+------+----------+-------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+------------+------------+--------+---------------+---------+---------+-------+------+----------+-------------+ | 1 | PRIMARY | <derived3> | NULL | system | NULL | NULL | NULL | NULL | 1 | 100.00 | NULL | | 3 | DERIVED | film | NULL | const | PRIMARY | PRIMARY | 4 | const | 1 | 100.00 | NULL | | 2 | SUBQUERY | actor | NULL | const | PRIMARY | PRIMARY | 4 | const | 1 | 100.00 | Using index | +----+-------------+------------+------------+--------+---------------+---------+---------+-------+------+----------+-------------+ 3 rows in set, 1 warning (0.00 sec)mysql>union
在 union 中的第二個(gè)和隨后的 select
mysql> EXPLAIN select 1 union select 1 ; +----+--------------+------------+------------+------+---------------+------+---------+------+------+----------+-----------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+--------------+------------+------------+------+---------------+------+---------+------+------+----------+-----------------+ | 1 | PRIMARY | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | No tables used | | 2 | UNION | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | No tables used | | NULL | UNION RESULT | <union1,2> | NULL | ALL | NULL | NULL | NULL | NULL | NULL | NULL | Using temporary | +----+--------------+------------+------------+------+---------------+------+---------+------+------+----------+-----------------+ 3 rows in set, 1 warning (0.00 sec)mysql>table
表示 explain 的一行正在訪問(wèn)哪個(gè)表
mysql> explain select (select 1 from actor where id = 1 ) from (select * from film where id =1 ) t ; +----+-------------+------------+------------+--------+---------------+---------+---------+-------+------+----------+-------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+------------+------------+--------+---------------+---------+---------+-------+------+----------+-------------+ | 1 | PRIMARY | <derived3> | NULL | system | NULL | NULL | NULL | NULL | 1 | 100.00 | NULL | | 3 | DERIVED | film | NULL | const | PRIMARY | PRIMARY | 4 | const | 1 | 100.00 | NULL | | 2 | SUBQUERY | actor | NULL | const | PRIMARY | PRIMARY | 4 | const | 1 | 100.00 | Using index | +----+-------------+------------+------------+--------+---------------+---------+---------+-------+------+----------+-------------+ 3 rows in set, 1 warning (0.00 sec)當(dāng) from 子句中有子查詢時(shí),table列是 <derivenN> 格式,表示當(dāng)前查詢依賴 id=N 的查詢,于是先執(zhí)行 id=N 的查詢。
mysql> EXPLAIN select 1 union select 1 ; +----+--------------+------------+------------+------+---------------+------+---------+------+------+----------+-----------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+--------------+------------+------------+------+---------------+------+---------+------+------+----------+-----------------+ | 1 | PRIMARY | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | No tables used | | 2 | UNION | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | No tables used | | NULL | UNION RESULT | <union1,2> | NULL | ALL | NULL | NULL | NULL | NULL | NULL | NULL | Using temporary | +----+--------------+------------+------------+------+---------------+------+---------+------+------+----------+-----------------+ 3 rows in set, 1 warning (0.00 sec)mysql>當(dāng)有 union 時(shí),UNION RESULT 的 table 列的值為<union1,2>,1和2表示參與 union 的 select 行id。
type
表示關(guān)聯(lián)類型或訪問(wèn)類型,即MySQL決定如何查找表中的行,查找數(shù)據(jù)行記錄的大概范圍。
依次從最優(yōu)到最差分別為:system > const > eq_ref > ref > range > index > ALL
一般來(lái)說(shuō),得保證查詢達(dá)到range級(jí)別,最好達(dá)到ref
NULL
mysql能夠在優(yōu)化階段分解查詢語(yǔ)句,在執(zhí)行階段用不著再訪問(wèn)表或索引.
例如:在索引列中選取最小值,可以單獨(dú)查找索引來(lái)完成,不需要在執(zhí)行時(shí)訪問(wèn)表
mysql> explain select min(id) from actor; +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+------------------------------+ | 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, 1 warning (0.00 sec)const, system
mysql能對(duì)查詢的某部分進(jìn)行優(yōu)化并將其轉(zhuǎn)化成一個(gè)常量(可以看show warnings 的結(jié)果)。用于primary key 或 unique key 的所有列與常數(shù)比較時(shí),所以表最多有一個(gè)匹配行,讀取1次,速度比較快。
system是const的特例,表里只有一條元組匹配時(shí)為system.
mysql> EXPLAIN select * from (select * from film where id=1) t ; +----+-------------+------------+------------+--------+---------------+---------+---------+-------+------+----------+-------+ | 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.00 | NULL | | 2 | DERIVED | film | NULL | const | PRIMARY | PRIMARY | 4 | const | 1 | 100.00 | NULL | +----+-------------+------------+------------+--------+---------------+---------+---------+-------+------+----------+-------+ 2 rows in set, 1 warning (0.00 sec)mysql> show warnings; +-------+------+---------------------------------------------------------------+ | Level | Code | Message | +-------+------+---------------------------------------------------------------+ | Note | 1003 | /* select#1 */ select '1' AS `id`,'film1' AS `name` from dual | +-------+------+---------------------------------------------------------------+ 1 row in set (0.00 sec)mysql>eq_ref
primary key 或 unique key 索引的所有部分被連接使用 ,最多只會(huì)返回一條符合條件的記錄。這可能是在const 之外最好的聯(lián)接類型了,簡(jiǎn)單的 select 查詢不會(huì)出現(xiàn)這種 type。
mysql> EXPLAIN select * from film_actor a left join film b on a.film_id = b.id ; +----+-------------+-------+------------+--------+---------------+---------+---------+------------------+------+----------+-------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+-------+------------+--------+---------------+---------+---------+------------------+------+----------+-------+ | 1 | SIMPLE | a | NULL | ALL | NULL | NULL | NULL | NULL | 3 | 100.00 | NULL | | 1 | SIMPLE | b | NULL | eq_ref | PRIMARY | PRIMARY | 4 | dbtest.a.film_id | 1 | 100.00 | NULL | +----+-------------+-------+------------+--------+---------------+---------+---------+------------------+------+----------+-------+ 2 rows in set, 1 warning (0.00 sec)mysql> show warnings; +-------+------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Level | Code | Message | +-------+------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Note | 1003 | /* select#1 */ select `dbtest`.`a`.`id` AS `id`,`dbtest`.`a`.`film_id` AS `film_id`,`dbtest`.`a`.`actor_id` AS `actor_id`,`dbtest`.`a`.`remark` AS `remark`,`dbtest`.`b`.`id` AS `id`,`dbtest`.`b`.`name` AS `name` from `dbtest`.`film_actor` `a` left join `dbtest`.`film` `b` on((`dbtest`.`b`.`id` = `dbtest`.`a`.`film_id`)) where 1 | +-------+------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec)mysql>ref
相比 eq_ref,不使用唯一索引,而是使用普通索引或者唯一性索引的部分前綴,索引要和某個(gè)值相比較,可能會(huì)找到多個(gè)符合條件的行.
【 簡(jiǎn)單 select 查詢,name是普通索引(非唯一索引)】
mysql> show INDEX from film ; +-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ | Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | +-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ | film | 0 | PRIMARY | 1 | id | A | 3 | NULL | NULL | | BTREE | | | | film | 1 | idx_name | 1 | name | A | 3 | NULL | NULL | YES | BTREE | | | +-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 2 rows in set (0.00 sec)mysql> mysql> EXPLAIN select * from film a where a.name = 'film0'; +----+-------------+-------+------------+------+---------------+----------+---------+-------+------+----------+-------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+-------+------------+------+---------------+----------+---------+-------+------+----------+-------------+ | 1 | SIMPLE | a | NULL | ref | idx_name | idx_name | 33 | const | 1 | 100.00 | Using index | +----+-------------+-------+------------+------+---------------+----------+---------+-------+------+----------+-------------+ 1 row in set, 1 warning (0.00 sec)mysql>【關(guān)聯(lián)表查詢,idx_film_actor_id是film_id和actor_id的聯(lián)合索引,這里使用到了film_actor的左邊前綴film_id部分?!?/p> mysql> show index from film_actor; +------------+------------+-------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ | Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | +------------+------------+-------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ | film_actor | 0 | PRIMARY | 1 | id | A | 3 | NULL | NULL | | BTREE | | | | film_actor | 1 | idx_film_actor_id | 1 | film_id | A | 2 | NULL | NULL | | BTREE | | | | film_actor | 1 | idx_film_actor_id | 2 | actor_id | A | 3 | NULL | NULL | | BTREE | | | +------------+------------+-------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 3 rows in set (0.00 sec)mysql> mysql> EXPLAIN select film_id from film left join film_actor on film.id =film_actor.film_id ; +----+-------------+------------+------------+-------+-------------------+-------------------+---------+----------------+------+----------+-------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+------------+------------+-------+-------------------+-------------------+---------+----------------+------+----------+-------------+ | 1 | SIMPLE | film | NULL | index | NULL | idx_name | 33 | NULL | 3 | 100.00 | Using index | | 1 | SIMPLE | film_actor | NULL | ref | idx_film_actor_id | idx_film_actor_id | 4 | dbtest.film.id | 1 | 100.00 | Using index | +----+-------------+------------+------------+-------+-------------------+-------------------+---------+----------------+------+----------+-------------+ 2 rows in set, 1 warning (0.00 sec)mysql>
range
范圍掃描通常出現(xiàn)在 in(), between ,> ,<, >= 等操作中。使用一個(gè)索引來(lái)檢索給定范圍的行。
mysql> explain select * from actor where id > 1 ; +----+-------------+-------+------------+-------+---------------+---------+---------+------+------+----------+-------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+-------+------------+-------+---------------+---------+---------+------+------+----------+-------------+ | 1 | SIMPLE | actor | NULL | range | PRIMARY | PRIMARY | 4 | NULL | 2 | 100.00 | Using where | +----+-------------+-------+------------+-------+---------------+---------+---------+------+------+----------+-------------+ 1 row in set, 1 warning (0.00 sec)index
掃描全索引就能拿到結(jié)果,一般是掃描某個(gè)二級(jí)索引,這種掃描不會(huì)從索引樹(shù)根節(jié)點(diǎn)開(kāi)始快速查找,而是直接對(duì)二級(jí)索引的葉子節(jié)點(diǎn)遍歷和掃描,速度還是比較慢的,這種查詢一般為使用覆蓋索引,二級(jí)索引一般比較小,所以這種通常比ALL快一些
mysql> explain select * from film; +----+-------------+-------+------------+-------+---------------+----------+---------+------+------+----------+-------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+-------+------------+-------+---------------+----------+---------+------+------+----------+-------------+ | 1 | SIMPLE | film | NULL | index | NULL | idx_name | 33 | NULL | 3 | 100.00 | Using index | +----+-------------+-------+------------+-------+---------------+----------+---------+------+------+----------+-------------+ 1 row in set, 1 warning (0.00 sec)ALL
全表掃描,掃描你的聚簇索引的所有葉子節(jié)點(diǎn)。通常情況下這需要增加索引來(lái)進(jìn)行優(yōu)化了
mysql> explain select * from actor ; +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------+ | 1 | SIMPLE | actor | NULL | ALL | NULL | NULL | NULL | NULL | 3 | 100.00 | NULL | +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------+ 1 row in set, 1 warning (0.00 sec)possible_keys
顯示查詢可能使用哪些索引來(lái)查找
explain 時(shí)可能出現(xiàn) possible_keys 有列,而 key 顯示 NULL 的情況,這種情況是因?yàn)楸碇袛?shù)據(jù)不多,mysql認(rèn)為索引對(duì)此查詢幫助不大,選擇了全表查詢。
如果該列是NULL,則沒(méi)有相關(guān)的索引。在這種情況下,可以通過(guò)檢查 where 子句看是否可以創(chuàng)造一個(gè)適當(dāng)?shù)乃饕齺?lái)提高查詢性能,然后用 explain 查看效果。
key
mysql實(shí)際采用哪個(gè)索引來(lái)優(yōu)化對(duì)該表的訪問(wèn)。
如果沒(méi)有使用索引,則該列是 NULL。如果想強(qiáng)制mysql使用或忽視possible_keys列中的索引,在查詢中使用 force index、ignore index。
key_len
顯示了mysql在索引里使用的字節(jié)數(shù),通過(guò)這個(gè)值可以算出具體使用了索引中的哪些列。
舉個(gè)例子 :
film_actor的聯(lián)合索引 idx_film_actor_id 由 film_id 和 actor_id 兩個(gè)int列組成,并且每個(gè)int是4字節(jié)。
mysql> explain select * from film_actor where film_id=1; +----+-------------+------------+------------+------+-------------------+-------------------+---------+-------+------+----------+-------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+------------+------------+------+-------------------+-------------------+---------+-------+------+----------+-------+ | 1 | SIMPLE | film_actor | NULL | ref | idx_film_actor_id | idx_film_actor_id | 4 | const | 2 | 100.00 | NULL | +----+-------------+------------+------------+------+-------------------+-------------------+---------+-------+------+----------+-------+ 1 row in set, 1 warning (0.00 sec)mysql>通過(guò)結(jié)果中的key_len=4可推斷出查詢使用了第一個(gè)列:film_id列來(lái)執(zhí)行索引查找。
key_len計(jì)算規(guī)則
【字符串】
- char(n):n字節(jié)長(zhǎng)度
- varchar(n):如果是utf-8,則長(zhǎng)度 3n + 2 字節(jié),加的2字節(jié)用來(lái)存儲(chǔ)字符串長(zhǎng)度
【數(shù)值類型】
- tinyint:1字節(jié)
- smallint:2字節(jié)
- int:4字節(jié)
- bigint:8字節(jié)
【時(shí)間類型】
- date:3字節(jié)
- timestamp:4字節(jié)
- datetime:8字節(jié)
如果字段允許為 NULL,需要1字節(jié)記錄是否為 NULL
索引最大長(zhǎng)度是768字節(jié),當(dāng)字符串過(guò)長(zhǎng)時(shí),mysql會(huì)做一個(gè)類似左前綴索引的處理,將前半部分的字符提取出來(lái)做索引
ref
顯示了在key列記錄的索引中,表查找值所用到的列或常量,常見(jiàn)的有:const(常量),字段名(例:film.id)
mysql> explain select * from film_actor where film_id=1; +----+-------------+------------+------------+------+-------------------+-------------------+---------+-------+------+----------+-------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+------------+------------+------+-------------------+-------------------+---------+-------+------+----------+-------+ | 1 | SIMPLE | film_actor | NULL | ref | idx_film_actor_id | idx_film_actor_id | 4 | const | 2 | 100.00 | NULL | +----+-------------+------------+------------+------+-------------------+-------------------+---------+-------+------+----------+-------+ 1 row in set, 1 warning (0.00 sec) mysql> EXPLAIN select film_id from film left join film_actor on film.id =film_actor.film_id ; +----+-------------+------------+------------+-------+-------------------+-------------------+---------+----------------+------+----------+-------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+------------+------------+-------+-------------------+-------------------+---------+----------------+------+----------+-------------+ | 1 | SIMPLE | film | NULL | index | NULL | idx_name | 33 | NULL | 3 | 100.00 | Using index | | 1 | SIMPLE | film_actor | NULL | ref | idx_film_actor_id | idx_film_actor_id | 4 | dbtest.film.id | 1 | 100.00 | Using index | +----+-------------+------------+------------+-------+-------------------+-------------------+---------+----------------+------+----------+-------------+ 2 rows in set, 1 warning (0.00 sec)rows
mysql估計(jì)要讀取并檢測(cè)的行數(shù),注意這個(gè)不是結(jié)果集里的行數(shù)。
Extra
展示的是額外信息。
列舉幾個(gè)常見(jiàn)的值
Using index
使用覆蓋索引 : 無(wú)需回表
mysql執(zhí)行計(jì)劃explain結(jié)果里的key有使用索引,如果select后面查詢的字段都可以從這個(gè)索引的樹(shù)中獲取,這種情況一般可以說(shuō)是用到了覆蓋索引,extra里一般都有using index;
覆蓋索引一般針對(duì)的是輔助索引,整個(gè)查詢結(jié)果只通過(guò)輔助索引就能拿到結(jié)果,不需要通過(guò)輔助索引樹(shù)找到主鍵,再通過(guò)主鍵去主鍵索引樹(shù)里獲取其它字段值
mysql> explain select film_id from film_actor where film_id=1; +----+-------------+------------+------------+------+-------------------+-------------------+---------+-------+------+----------+-------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+------------+------------+------+-------------------+-------------------+---------+-------+------+----------+-------------+ | 1 | SIMPLE | film_actor | NULL | ref | idx_film_actor_id | idx_film_actor_id | 4 | const | 2 | 100.00 | Using index | +----+-------------+------------+------------+------+-------------------+-------------------+---------+-------+------+----------+-------------+ 1 row in set, 1 warning (0.00 sec)mysql>Using where
使用 where 語(yǔ)句來(lái)處理結(jié)果,并且查詢的列未被索引覆蓋
mysql> show index from actor; +-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ | Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | +-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ | actor | 0 | PRIMARY | 1 | id | A | 3 | NULL | NULL | | BTREE | | | +-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 1 row in set (0.00 sec)mysql> explain select * from actor where name = 'a'; +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+ | 1 | SIMPLE | actor | NULL | ALL | NULL | NULL | NULL | NULL | 3 | 33.33 | Using where | +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+ 1 row in set, 1 warning (0.00 sec)mysql>Using index condition
查詢的列不完全被索引覆蓋,where條件中是一個(gè)前導(dǎo)列的范圍;
mysql> explain select * from film_actor where film_id > 1 ; +----+-------------+------------+------------+-------+-------------------+-------------------+---------+------+------+----------+-----------------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+------------+------------+-------+-------------------+-------------------+---------+------+------+----------+-----------------------+ | 1 | SIMPLE | film_actor | NULL | range | idx_film_actor_id | idx_film_actor_id | 4 | NULL | 1 | 100.00 | Using index condition | +----+-------------+------------+------------+-------+-------------------+-------------------+---------+------+------+----------+-----------------------+ 1 row in set, 1 warning (0.00 sec)mysql>Using temporary
mysql需要?jiǎng)?chuàng)建一張臨時(shí)表來(lái)處理查詢。出現(xiàn)這種情況一般是要進(jìn)行優(yōu)化的,首先是想到用索引來(lái)優(yōu)化。
【actor.name沒(méi)有索引,此時(shí)創(chuàng)建了張臨時(shí)表來(lái)distinct】
mysql> explain select distinct name from actor; +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-----------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-----------------+ | 1 | SIMPLE | actor | NULL | ALL | NULL | NULL | NULL | NULL | 3 | 100.00 | Using temporary | +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-----------------+ 1 row in set, 1 warning (0.00 sec)【film.name建立了idx_name索引,此時(shí)查詢時(shí)extra是using index,沒(méi)有用臨時(shí)表】
mysql> explain select distinct name from film; +----+-------------+-------+------------+-------+---------------+----------+---------+------+------+----------+-------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+-------+------------+-------+---------------+----------+---------+------+------+----------+-------------+ | 1 | SIMPLE | film | NULL | index | idx_name | idx_name | 33 | NULL | 3 | 100.00 | Using index | +----+-------------+-------+------------+-------+---------------+----------+---------+------+------+----------+-------------+ 1 row in set, 1 warning (0.00 sec)mysql>Using filesort
將用外部排序而不是索引排序,數(shù)據(jù)較小時(shí)從內(nèi)存排序,否則需要在磁盤完成排序。這種情況下一般也是要考慮使用索引來(lái)優(yōu)化的
【 actor.name未創(chuàng)建索引,會(huì)瀏覽actor整個(gè)表,保存排序關(guān)鍵字name和對(duì)應(yīng)的id,然后排序name并檢索行記錄】
mysql> explain select * from actor order by name; +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+----------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+----------------+ | 1 | SIMPLE | actor | NULL | ALL | NULL | NULL | NULL | NULL | 3 | 100.00 | Using filesort | +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+----------------+ 1 row in set, 1 warning (0.00 sec)mysql>【film.name建立了idx_name索引,此時(shí)查詢時(shí)extra是using index】
mysql> explain select * from film order by name ; +----+-------------+-------+------------+-------+---------------+----------+---------+------+------+----------+-------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+-------+------------+-------+---------------+----------+---------+------+------+----------+-------------+ | 1 | SIMPLE | film | NULL | index | NULL | idx_name | 33 | NULL | 3 | 100.00 | Using index | +----+-------------+-------+------------+-------+---------------+----------+---------+------+------+----------+-------------+ 1 row in set, 1 warning (0.00 sec)mysql>Select tables optimized away
使用某些聚合函數(shù)(比如 max、min)來(lái)訪問(wèn)存在索引的某個(gè)字段
搞定MySQL實(shí)戰(zhàn)
總結(jié)
以上是生活随笔為你收集整理的MySQL - Explain深度剖析的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: MySQL - MySQL不同存储引擎
- 下一篇: MySQL - 践行索引优化