序言
? Explain
語句提供了mysql如何執(zhí)行語句的信息攒至,包括select
,delete
,insert
, replace
, update
Explain輸出的列
Column | JSON Name | Meaning |
---|---|---|
id |
select_id |
The SELECT identifier |
select_type |
None | The SELECT type |
table |
table_name |
The table for the output row |
partitions |
partitions |
The matching partitions |
type |
access_type |
The join type |
possible_keys |
possible_keys |
The possible indexes to choose |
key |
key |
The index actually chosen |
key_len |
key_length |
The length of the chosen key |
ref |
ref |
The columns compared to the index |
rows |
rows |
Estimate(估算) of rows to be examined(檢查) |
filtered |
filtered |
Percentage(百分比) of rows filtered(過濾) by table condition |
Extra |
None | Additional information |
select_type
select
的類型竖幔,包含如下值:
Value | Meaning |
---|---|
SIMPLE |
Simple SELECT (not using UNION or subqueries) |
PRIMARY |
Outermost SELECT
|
UNION |
Second or later SELECT statement in a UNION
|
DEPENDENT UNION |
Second or later SELECT statement in a UNION , dependent on outer query |
UNION RESULT |
Result of a UNION . |
SUBQUERY |
First SELECT in subquery |
DEPENDENT SUBQUERY |
First SELECT in subquery, dependent on outer query |
DERIVED |
Derived table |
DEPENDENT DERIVED |
Derived table dependent on another table |
MATERIALIZED |
Materialized subquery |
UNCACHEABLE SUBQUERY |
A subquery for which the result cannot be cached and must be re-evaluated for each row of the outer query |
UNCACHEABLE UNION |
The second or later select in a UNION that belongs to an uncacheable subquery (see UNCACHEABLE SUBQUERY ) |
type
? 順序從好到差依次介紹如下:
system
:表只有一行惭墓。system
是const
的特例-
const
:const
用于將主鍵或唯一索引的所有部分與常量值進行=
測試,表最多有一個匹配行SELECT * FROM tbl_name WHERE primary_key=1; SELECT * FROM tbl_name WHERE primary_key_part1=1 AND primary_key_part2=2; --上述查詢是const類型的
-
eq_ref
:多表聯(lián)結(jié)中使用primary key
或者unique not null
的列,進行=
測試,比較值可以是常量嵌灰,也可以是在該表之前讀取的表中的列的表達式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; --上述查詢是eq_ref類型的
create table table_0(id_0 bigint primary key, v_0 int, index(v_0)); create table table_1(id_1 bigint primary key, v_1 int, index(v_1)); insert into table_0(id_0, v_0) values (0, 1), (1, 2); insert into table_1(id_1, v_1) values (0, 1), (1, 2); explain SELECT * from table_0, table_1 where table_0.id_0 = table_1.id_1
id select_type table partitions type possible_keys key key_len ref rows filtered extra 1 simple table_0 null index primary v_0 5 null 2 100 using index 1 simple table_1 null eq_ref primary primary 8 szn.table_0.id+0 1 100 null -
ref
:類似于eq_ref
,ref
操作的索引不是primary key
或者unique not null
颅悉,所以返回的行數(shù)可能大于1行(在索引列上使用的依然是=
操作)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; --上述查詢是ref類型的
explain SELECT * from table_0, table_1 where table_0.v_0 = table_1.v_1 --表結(jié)構(gòu)及包含的數(shù)據(jù)同eq_ref時舉的例子
id select_type table partitions type possible_keys key key_len ref rows filtered extra 1 simple table_0 null index v_0 v_0 5 null 2 100 Using where; Using index 1 simple table_1 null ref v_1 v_1 5 szn.table_0.id+0 1 100 Using index fulltext
:使用全文索引-
ref_or_null
:類似于ref
沽瞭,但是增加了搜索行是否為空的條件SELECT * FROM ref_table WHERE key_column=expr OR key_column IS NULL; --上述查詢是ref_or_null類型的
index_merge
:索引合并優(yōu)化被使用-
unique_subquery
:在以下形式的in
子查詢中,unique_subquery
代替了eq_ref
value IN (SELECT primary_key FROM single_table WHERE some_expr)
-
index_subquery
:類似于unique_subquery
剩瓶,但是作用的index
不是唯一索引value IN (SELECT key_column FROM single_table WHERE some_expr)
-
range
:使用索引來檢索一個范圍的行驹溃,支持的操作符:=
,<>
,>
,>=
,<
,<=
,IS NULL
,<=>
,BETWEEN
,LIKE
, orIN()
SELECT * FROM tbl_name WHERE key_column = 10; SELECT * FROM tbl_name WHERE key_column BETWEEN 10 and 20; SELECT * FROM tbl_name WHERE key_column IN (10,20,30); SELECT * FROM tbl_name WHERE key_part1 = 10 AND key_part2 IN (10,20,30);
-
index
:將掃描索引樹,有二種情況:- 如果查詢的索引是覆蓋索引延曙,則只有索引樹被掃描(速度很快)豌鹤。
Extra
列顯示using index
。 - 以索引順序搂鲫,通過讀取的索引傍药,執(zhí)行全表掃描來尋找數(shù)據(jù)磺平。
Extra
列不會顯示using index
- 如果查詢的索引是覆蓋索引延曙,則只有索引樹被掃描(速度很快)豌鹤。
all
:執(zhí)行全表掃描魂仍。應(yīng)盡量避免這種類型
key
-
mysql
實際決定使用的key
- 有可能不會出現(xiàn)在
possible_keys
的列表中
ref
? ref
指明了和key
比較的是那一列或者常量
create table table_0(id_0 bigint primary key, v_0 int, index(v_0));
insert into table_0(id_0, v_0) values (0, 1), (1, 2);
explain SELECT * from table_0 where table_0.id_0 = 1
--ref列拐辽,值為 const
explain SELECT * from table_0 where id_0 = v_0
--ref列,值為 v_0
rows
-
mysql
認(rèn)為執(zhí)行查詢必須檢查的行數(shù) - 對于
Innodb
擦酌,這個值是一個估算值俱诸,并不準(zhǔn)確
filtered
- 根據(jù)查詢條件,未過濾的行數(shù)的百分比
- 100表示未進行過濾
- 此值是一個預(yù)估值