面試官:不會(huì)看 Explain執(zhí)行計(jì)劃,簡(jiǎn)歷敢寫(xiě) SQL 優(yōu)化?
掃描二維碼
隨時(shí)隨地手機(jī)看文章
來(lái)自:程序員內(nèi)點(diǎn)事
昨天中午在食堂,和部門(mén)的技術(shù)大牛們坐在一桌吃飯,作為一個(gè)卑微技術(shù)渣仔默默的吃著飯,聽(tīng)大佬們高談闊論,研究各種高端技術(shù),我TM也想說(shuō)話可實(shí)在插不上嘴。
聊著聊著突然說(shuō)到他上午面試了一個(gè)工作6年的程序員,表情挺復(fù)雜,他說(shuō):我看他簡(jiǎn)歷寫(xiě)著熟悉SQL
語(yǔ)句調(diào)優(yōu),就問(wèn)了下 Explain
執(zhí)行計(jì)劃怎么看?結(jié)果這老哥一問(wèn)三不知,工作6年這么基礎(chǔ)的東西都不了解!
感受到了大佬的王之鄙視,回到工位我就開(kāi)始默默寫(xiě)這個(gè),哎~ 我TM也不太懂 Explain
,老哥你這是針對(duì)我?。】捱筮髜
Explain有什么用
當(dāng)Explain
與 SQL
語(yǔ)句一起使用時(shí),MySQL
會(huì)顯示來(lái)自優(yōu)化器關(guān)于SQL執(zhí)行的信息。也就是說(shuō),MySQL
解釋了它將如何處理該語(yǔ)句,包括如何連接表以及什么順序連接表等。
-
表的加載順序 -
sql
的查詢類型 -
可能用到哪些索引,哪些索引又被實(shí)際使用 -
表與表之間的引用關(guān)系 -
一個(gè)表中有多少行被優(yōu)化器查詢 .....
Explain有哪些信息
Explain
執(zhí)行計(jì)劃包含字段信息如下:分別是 id
、select_type
、table
、partitions
、type
、possible_keys
、key
、key_len
、ref
、rows
、filtered
、Extra
12個(gè)字段。下邊我們會(huì)結(jié)合具體的
SQL
示例,詳細(xì)的解讀每個(gè)字段以及每個(gè)字段中不同參數(shù)的含義,以下所有示例數(shù)據(jù)庫(kù)版本為 MySQL.5.7.17
。
mysql> select version() from dual;
+------------+
| version() |
+------------+
| 5.7.17-log |
+------------+
我們創(chuàng)建三張表 one
、two
、three
,表之間的關(guān)系 one.two_id = two.two_id AND two.three_id = three.three_id
。
Explain執(zhí)行計(jì)劃詳解
一、id
id:
:表示查詢中執(zhí)行select子句或者操作表的順序,id
的值越大,代表優(yōu)先級(jí)越高,越先執(zhí)行。 id
大致會(huì)出現(xiàn) 3種情況:
1、id
相同
看到三條記錄的id
都相同,可以理解成這三個(gè)表為一組,具有同樣的優(yōu)先級(jí),執(zhí)行順序由上而下,具體順序由優(yōu)化器決定。
mysql> EXPLAIN SELECT * FROM one o,two t, three r WHERE o.two_id = t.two_id AND t.three_id = r.three_id;
+----+-------------+-------+------------+--------+---------------+---------+---------+----------------------+------+----------+----------------------------------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+--------+---------------+---------+---------+----------------------+------+----------+----------------------------------------------------+
| 1 | SIMPLE | o | NULL | ALL | NULL | NULL | NULL | NULL | 2 | 100 | NULL |
| 1 | SIMPLE | t | NULL | ALL | PRIMARY | NULL | NULL | NULL | 2 | 50 | Using where; Using join buffer (Block Nested Loop) |
| 1 | SIMPLE | r | NULL | eq_ref | PRIMARY | PRIMARY | 4 | xin-slave.t.three_id | 1 | 100 | NULL |
+----+-------------+-------+------------+--------+---------------+---------+---------+----------------------+------+----------+----------------------------------------------------+
2、id
不同
如果我們的 SQL
中存在子查詢,那么 id
的序號(hào)會(huì)遞增,id
值越大優(yōu)先級(jí)越高,越先被執(zhí)行 。當(dāng)三個(gè)表依次嵌套,發(fā)現(xiàn)最里層的子查詢 id
最大,最先執(zhí)行。
mysql> EXPLAIN select * from one o where o.two_id = (select t.two_id from two t where t.three_id = (select r.three_id from three r where r.three_name='我是第三表2'));
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+
| 1 | PRIMARY | o | NULL | ALL | NULL | NULL | NULL | NULL | 2 | 50 | Using where |
| 2 | SUBQUERY | t | NULL | ALL | NULL | NULL | NULL | NULL | 2 | 50 | Using where |
| 3 | SUBQUERY | r | NULL | ALL | NULL | NULL | NULL | NULL | 2 | 50 | Using where |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+
3、以上兩種同時(shí)存在
將上邊的 SQL
稍微修改一下,增加一個(gè)子查詢,發(fā)現(xiàn) id
的以上兩種同時(shí)存在。相同id
劃分為一組,這樣就有三個(gè)組,同組的從上往下順序執(zhí)行,不同組 id
值越大,優(yōu)先級(jí)越高,越先執(zhí)行。
mysql> EXPLAIN select * from one o where o.two_id = (select t.two_id from two t where t.three_id = (select r.three_id from three r where r.three_name='我是第三表2')) AND o.one_id in(select one_id from one where o.one_name="我是第一表2");
+----+-------------+-------+------------+--------+---------------+---------+---------+--------------------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+--------+---------------+---------+---------+--------------------+------+----------+-------------+
| 1 | PRIMARY | o | NULL | ALL | PRIMARY | NULL | NULL | NULL | 2 | 50 | Using where |
| 1 | PRIMARY | one | NULL | eq_ref | PRIMARY | PRIMARY | 4 | xin-slave.o.one_id | 1 | 100 | Using index |
| 2 | SUBQUERY | t | NULL | ALL | NULL | NULL | NULL | NULL | 2 | 50 | Using where |
| 3 | SUBQUERY | r | NULL | ALL | NULL | NULL | NULL | NULL | 2 | 50 | Using where |
+----+-------------+-------+------------+--------+---------------+---------+---------+--------------------+------+----------+-------------+
二、select_type
select_type
:表示 select
查詢的類型,主要是用于區(qū)分各種復(fù)雜的查詢,例如:普通查詢
、聯(lián)合查詢
、子查詢
等。
1、SIMPLE
SIMPLE
:表示最簡(jiǎn)單的 select 查詢語(yǔ)句,也就是在查詢中不包含子查詢或者 union
交并差集等操作。
2、PRIMARY
PRIMARY
:當(dāng)查詢語(yǔ)句中包含任何復(fù)雜的子部分,最外層查詢則被標(biāo)記為PRIMARY
。
3、SUBQUERY
SUBQUERY
:當(dāng) select
或 where
列表中包含了子查詢,該子查詢被標(biāo)記為:SUBQUERY
。
4、DERIVED
DERIVED
:表示包含在from
子句中的子查詢的select,在我們的 from
列表中包含的子查詢會(huì)被標(biāo)記為derived
。
5、UNION
UNION
:如果union
后邊又出現(xiàn)的select
語(yǔ)句,則會(huì)被標(biāo)記為union
;若 union
包含在 from
子句的子查詢中,外層 select
將被標(biāo)記為 derived
。
6、UNION RESULT
UNION RESULT
:代表從union
的臨時(shí)表中讀取數(shù)據(jù),而table
列的<union1,4>
表示用第一個(gè)和第四個(gè)select
的結(jié)果進(jìn)行union
操作。
mysql> EXPLAIN select t.two_name, ( select one.one_id from one) o from (select two_id,two_name from two where two_name ='') t union (select r.three_name,r.three_id from three r);
+------+--------------+------------+------------+-------+---------------+---------+---------+------+------+----------+-----------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+------+--------------+------------+------------+-------+---------------+---------+---------+------+------+----------+-----------------+
| 1 | PRIMARY | two | NULL | ALL | NULL | NULL | NULL | NULL | 2 | 50 | Using where |
| 2 | SUBQUERY | one | NULL | index | NULL | PRIMARY | 4 | NULL | 2 | 100 | Using index |
| 4 | UNION | r | NULL | ALL | NULL | NULL | NULL | NULL | 2 | 100 | NULL |
| NULL | UNION RESULT | <union1,4> | NULL | ALL | NULL | NULL | NULL | NULL | NULL | NULL | Using temporary |
+------+--------------+------------+------------+-------+---------------+---------+---------+------+------+----------+-----------------+
三、table
查詢的表名,并不一定是真實(shí)存在的表,有別名顯示別名,也可能為臨時(shí)表,例如上邊的DERIVED
、 <union1,4>
等。
四、partitions
查詢時(shí)匹配到的分區(qū)信息,對(duì)于非分區(qū)表值為NULL
,當(dāng)查詢的是分區(qū)表時(shí),partitions
顯示分區(qū)表命中的分區(qū)情況。
+----+-------------+----------------+---------------------------------+-------+---------------+---------+---------+------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+----------------+---------------------------------+-------+---------------+---------+---------+------+------+----------+-------------+
| 1 | SIMPLE | one | p201801,p201802,p201803,p300012 | index | NULL | PRIMARY | 9 | NULL | 3 | 100 | Using index |
+----+-------------+----------------+---------------------------------+-------+---------------+---------+---------+------+------+----------+-------------+
五、type
type
:查詢使用了何種類型,它在 SQL
優(yōu)化中是一個(gè)非常重要的指標(biāo),以下性能從好到壞依次是:system
> const
> eq_ref
> ref
> ref_or_null
> index_merge
> unique_subquery
> index_subquery
> range
> index
> ALL
1、system
system
: 當(dāng)表僅有一行記錄時(shí)(系統(tǒng)表),數(shù)據(jù)量很少,往往不需要進(jìn)行磁盤(pán)IO,速度非??臁?/span>
2、const
const
:表示查詢時(shí)命中 primary key
主鍵或者 unique
唯一索引,或者被連接的部分是一個(gè)常量(const
)值。這類掃描效率極高,返回?cái)?shù)據(jù)量少,速度非???。
mysql> EXPLAIN SELECT * from three where three_id=1;
+----+-------------+-------+------------+-------+---------------+---------+---------+-------+------+----------+-------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+-------+---------------+---------+---------+-------+------+----------+-------+
| 1 | SIMPLE | three | NULL | const | PRIMARY | PRIMARY | 4 | const | 1 | 100 | NULL |
+----+-------------+-------+------------+-------+---------------+---------+---------+-------+------+----------+-------+
3、eq_ref
eq_ref
:查詢時(shí)命中主鍵primary key
或者 unique key
索引, type
就是 eq_ref
。
mysql> EXPLAIN select o.one_name from one o ,two t where o.one_id = t.two_id ;
+----+-------------+-------+------------+--------+---------------+----------+---------+--------------------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+--------+---------------+----------+---------+--------------------+------+----------+-------------+
| 1 | SIMPLE | o | NULL | index | PRIMARY | idx_name | 768 | NULL | 2 | 100 | Using index |
| 1 | SIMPLE | t | NULL | eq_ref | PRIMARY | PRIMARY | 4 | xin-slave.o.one_id | 1 | 100 | Using index |
+----+-------------+-------+------------+--------+---------------+----------+---------+--------------------+------+----------+-------------+
4、ref
ref
:區(qū)別于eq_ref
,ref
表示使用非唯一性索引,會(huì)找到很多個(gè)符合條件的行。
mysql> select o.one_id from one o where o.one_name = "xin" ;
+--------+
| one_id |
+--------+
| 1 |
| 3 |
+--------+```
```sql
mysql> EXPLAIN select o.one_id from one o where o.one_name = "xin" ;
+----+-------------+-------+------------+------+---------------+----------+---------+-------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+------+---------------+----------+---------+-------+------+----------+-------------+
| 1 | SIMPLE | o | NULL | ref | idx_name | idx_name | 768 | const | 1 | 100 | Using index |
+----+-------------+-------+------------+------+---------------+----------+---------+-------+------+----------+-------------+
5、ref_or_null
ref_or_null
:這種連接類型類似于 ref,區(qū)別在于 MySQL
會(huì)額外搜索包含NULL
值的行。
mysql> EXPLAIN select o.one_id from one o where o.one_name = "xin" OR o.one_name IS NULL;
+----+-------------+-------+------------+-------------+---------------+----------+---------+-------+------+----------+--------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+-------------+---------------+----------+---------+-------+------+----------+--------------------------+
| 1 | SIMPLE | o | NULL | ref_or_null | idx_name | idx_name | 768 | const | 3 | 100 | Using where; Using index |
+----+-------------+-------+------------+-------------+---------------+----------+---------+-------+------+----------+--------------------------+
6、index_merge
index_merge
:使用了索引合并優(yōu)化方法,查詢使用了兩個(gè)以上的索引。
下邊示例中同時(shí)使用到主鍵one_id
和 字段one_name
的idx_name
索引 。
mysql> EXPLAIN select * from one o where o.one_id >1 and o.one_name ='xin';
+----+-------------+-------+------------+-------------+------------------+------------------+---------+------+------+----------+------------------------------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+-------------+------------------+------------------+---------+------+------+----------+------------------------------------------------+
| 1 | SIMPLE | o | NULL | index_merge | PRIMARY,idx_name | idx_name,PRIMARY | 772,4 | NULL | 1 | 100 | Using intersect(idx_name,PRIMARY); Using where |
+----+-------------+-------+------------+-------------+------------------+------------------+---------+------+------+----------+------------------------------------------------+
7、unique_subquery
unique_subquery
:替換下面的 IN
子查詢,子查詢返回不重復(fù)的集合。
value IN (SELECT primary_key FROM single_table WHERE some_expr)
8、index_subquery
index_subquery
:區(qū)別于unique_subquery
,用于非唯一索引,可以返回重復(fù)值。
value IN (SELECT key_column FROM single_table WHERE some_expr)
9、range
range
:使用索引選擇行,僅檢索給定范圍內(nèi)的行。簡(jiǎn)單點(diǎn)說(shuō)就是針對(duì)一個(gè)有索引的字段,給定范圍檢索數(shù)據(jù)。在where
語(yǔ)句中使用 bettween...and
、<
、>
、<=
、in
等條件查詢 type
都是 range
。
舉個(gè)栗子:three
表中three_id
為唯一主鍵,user_id
普通字段未建索引。
mysql> EXPLAIN SELECT * from three where three_id BETWEEN 2 AND 3;
+----+-------------+-------+------------+-------+---------------+---------+---------+------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+-------+---------------+---------+---------+------+------+----------+-------------+
| 1 | SIMPLE | three | NULL | range | PRIMARY | PRIMARY | 4 | NULL | 1 | 100 | Using where |
+----+-------------+-------+------------+-------+---------------+---------+---------+------+------+----------+-------------+
從結(jié)果中看到只有對(duì)設(shè)置了索引的字段,做范圍檢索 type
才是 range
。
mysql> EXPLAIN SELECT * from three where user_id BETWEEN 2 AND 3;
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+
| 1 | SIMPLE | three | NULL | ALL | NULL | NULL | NULL | NULL | 3 | 33.33 | Using where |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+
10、index
index
:Index
與ALL
其實(shí)都是讀全表,區(qū)別在于index
是遍歷索引樹(shù)讀取,而ALL
是從硬盤(pán)中讀取。
下邊示例:three_id
為主鍵,不帶 where
條件全表查詢 ,type
結(jié)果為index
。
mysql> EXPLAIN SELECT three_id from three ;
+----+-------------+-------+------------+-------+---------------+---------+---------+------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+-------+---------------+---------+---------+------+------+----------+-------------+
| 1 | SIMPLE | three | NULL | index | NULL | PRIMARY | 4 | NULL | 1 | 100 | Using index |
+----+-------------+-------+------------+-------+---------------+---------+---------+------+------+----------+-------------+
11、ALL
ALL
:將遍歷全表以找到匹配的行,性能最差。
mysql> EXPLAIN SELECT * from two ;
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------+
| 1 | SIMPLE | two | NULL | ALL | NULL | NULL | NULL | NULL | 2 | 100 | NULL |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------+
六、possible_keys
possible_keys
:表示在MySQL
中通過(guò)哪些索引,能讓我們?cè)诒碇姓业较胍挠涗?,一旦查詢涉及到的某個(gè)字段上存在索引,則索引將被列出,但這個(gè)索引并不定一會(huì)是最終查詢數(shù)據(jù)時(shí)所被用到的索引。具體請(qǐng)參考上邊的例子。
七、key
key
:區(qū)別于possible_keys
,key是查詢中實(shí)際使用到的索引,若沒(méi)有使用索引,顯示為NULL
。具體請(qǐng)參考上邊的例子。
當(dāng)
type
為index_merge
時(shí),可能會(huì)顯示多個(gè)索引。
八、key_len
key_len
:表示查詢用到的索引長(zhǎng)度(字節(jié)數(shù)),原則上長(zhǎng)度越短越好 。
-
單列索引,那么需要將整個(gè)索引長(zhǎng)度算進(jìn)去; -
多列索引,不是所有列都能用到,需要計(jì)算查詢中實(shí)際用到的列。
注意:
key_len
只計(jì)算where
條件中用到的索引長(zhǎng)度,而排序和分組即便是用到了索引,也不會(huì)計(jì)算到key_len
中。
九、ref
ref
:常見(jiàn)的有:const
,func
,null
,字段名。
-
當(dāng)使用常量等值查詢,顯示 const
, -
當(dāng)關(guān)聯(lián)查詢時(shí),會(huì)顯示相應(yīng)關(guān)聯(lián)表的 關(guān)聯(lián)字段
-
如果查詢條件使用了 表達(dá)式
、函數(shù)
,或者條件列發(fā)生內(nèi)部隱式轉(zhuǎn)換,可能顯示為func
-
其他情況 null
十、rows
rows
:以表的統(tǒng)計(jì)信息和索引使用情況,估算要找到我們所需的記錄,需要讀取的行數(shù)。
這是評(píng)估SQL
性能的一個(gè)比較重要的數(shù)據(jù),mysql
需要掃描的行數(shù),很直觀的顯示 SQL
性能的好壞,一般情況下 rows
值越小越好。
mysql> EXPLAIN SELECT * from three;
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------+
| 1 | SIMPLE | three | NULL | ALL | NULL | NULL | NULL | NULL | 3 | 100 | NULL |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------+
十一、filtered
filtered
這個(gè)是一個(gè)百分比的值,表里符合條件的記錄數(shù)的百分比。簡(jiǎn)單點(diǎn)說(shuō),這個(gè)字段表示存儲(chǔ)引擎返回的數(shù)據(jù)在經(jīng)過(guò)過(guò)濾后,剩下滿足條件的記錄數(shù)量的比例。
在MySQL.5.7
版本以前想要顯示filtered
需要使用explain extended
命令。MySQL.5.7
后,默認(rèn)explain
直接顯示partitions
和filtered
的信息。
十二、Extra
Extra
:不適合在其他列中顯示的信息,Explain
中的很多額外的信息會(huì)在 Extra
字段顯示。
1、Using index
Using index
:我們?cè)谙鄳?yīng)的 select
操作中使用了覆蓋索引,通俗一點(diǎn)講就是查詢的列被索引覆蓋,使用到覆蓋索引查詢速度會(huì)非??欤?/span>SQl
優(yōu)化中理想的狀態(tài)。
什么又是覆蓋索引?
一條 SQL
只需要通過(guò)索引就可以返回,我們所需要查詢的數(shù)據(jù)(一個(gè)或幾個(gè)字段),而不必通過(guò)二級(jí)索引,查到主鍵之后再通過(guò)主鍵查詢整行數(shù)據(jù)(select *
)。
one_id
表為主鍵
mysql> EXPLAIN SELECT one_id from one ;
+----+-------------+-------+------------+-------+---------------+------------+---------+------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+-------+---------------+------------+---------+------+------+----------+-------------+
| 1 | SIMPLE | one | NULL | index | NULL | idx_two_id | 5 | NULL | 3 | 100 | Using index |
+----+-------------+-------+------------+-------+---------------+------------+---------+------+------+----------+-------------+
注意:想要使用到覆蓋索引,我們?cè)?select
時(shí)只取出需要的字段,不可select *
,而且該字段建了索引。
mysql> EXPLAIN SELECT * from one ;
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------+
| 1 | SIMPLE | one | NULL | ALL | NULL | NULL | NULL | NULL | 3 | 100 | NULL |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------+
2、Using where
Using where
:查詢時(shí)未找到可用的索引,進(jìn)而通過(guò)where
條件過(guò)濾獲取所需數(shù)據(jù),但要注意的是并不是所有帶where
語(yǔ)句的查詢都會(huì)顯示Using where
。
下邊示例create_time
并未用到索引,type
為 ALL
,即MySQL
通過(guò)全表掃描后再按where
條件篩選數(shù)據(jù)。
mysql> EXPLAIN SELECT one_name from one where create_time ='2020-05-18';
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+
| 1 | SIMPLE | one | NULL | ALL | NULL | NULL | NULL | NULL | 3 | 33.33 | Using where |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+
3、Using temporary
Using temporary
:表示查詢后結(jié)果需要使用臨時(shí)表來(lái)存儲(chǔ),一般在排序或者分組查詢時(shí)用到。
mysql> EXPLAIN SELECT one_name from one where one_id in (1,2) group by one_name;
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+
| 1 | SIMPLE | one | NULL | range| NULL | NULL | NULL | NULL | 3 | 33.33 | Using where; Using temporary; Using filesort |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+
4、Using filesort
Using filesort
:表示無(wú)法利用索引完成的排序操作,也就是ORDER BY
的字段沒(méi)有索引,通常這樣的SQL都是需要優(yōu)化的。
mysql> EXPLAIN SELECT one_id from one ORDER BY create_time;
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+----------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+----------------+
| 1 | SIMPLE | one | NULL | ALL | NULL | NULL | NULL | NULL | 3 | 100 | Using filesort |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+----------------+
如果ORDER BY
字段有索引就會(huì)用到覆蓋索引,相比執(zhí)行速度快很多。
mysql> EXPLAIN SELECT one_id from one ORDER BY one_id;
+----+-------------+-------+------------+-------+---------------+---------+---------+------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+-------+---------------+---------+---------+------+------+----------+-------------+
| 1 | SIMPLE | one | NULL | index | NULL | PRIMARY | 4 | NULL | 3 | 100 | Using index |
+----+-------------+-------+------------+-------+---------------+---------+---------+------+------+----------+-------------+
5、Using join buffer
Using join buffer
:在我們聯(lián)表查詢的時(shí)候,如果表的連接條件沒(méi)有用到索引,需要有一個(gè)連接緩沖區(qū)來(lái)存儲(chǔ)中間結(jié)果。
先看一下有索引的情況:連接條件 one_name
、two_name
都用到索引。
mysql> EXPLAIN SELECT one_name from one o,two t where o.one_name = t.two_name;
+----+-------------+-------+------------+-------+---------------+----------+---------+----------------------+------+----------+--------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+-------+---------------+----------+---------+----------------------+------+----------+--------------------------+
| 1 | SIMPLE | o | NULL | index | idx_name | idx_name | 768 | NULL | 3 | 100 | Using where; Using index |
| 1 | SIMPLE | t | NULL | ref | idx_name | idx_name | 768 | xin-slave.o.one_name | 1 | 100 | Using index |
+----+-------------+-------+------------+-------+---------------+----------+---------+----------------------+------+----------+--------------------------+
接下來(lái)刪掉 連接條件 one_name
、two_name
的字段索引。發(fā)現(xiàn)Extra
列變成 Using join buffer
,type
均為全表掃描,這也是SQL
優(yōu)化中需要注意的地方。
mysql> EXPLAIN SELECT one_name from one o,two t where o.one_name = t.two_name;
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+----------------------------------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+----------------------------------------------------+
| 1 | SIMPLE | t | NULL | ALL | NULL | NULL | NULL | NULL | 2 | 100 | NULL |
| 1 | SIMPLE | o | NULL | ALL | NULL | NULL | NULL | NULL | 3 | 33.33 | Using where; Using join buffer (Block Nested Loop) |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+----------------------------------------------------+
6、Impossible where
Impossible where
:表示在我們用不太正確的where
語(yǔ)句,導(dǎo)致沒(méi)有符合條件的行。
mysql> EXPLAIN SELECT one_name from one WHERE 1=2;
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+------------------+
| 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 |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+------------------+
7、No tables used
No tables used
:我們的查詢語(yǔ)句中沒(méi)有FROM
子句,或者有 FROM DUAL
子句。
mysql> EXPLAIN select now();
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+----------------+
| 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 | No tables used |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+----------------+
Extra
列的信息非常非常多,這里就不再一一列舉了,詳見(jiàn) MySQL
官方文檔 :https://dev.mysql.com/doc/refman/5.7/en/explain-output.html#jointype_index_merge
總結(jié)
上邊只是簡(jiǎn)單介紹了下 Explain
執(zhí)行計(jì)劃各個(gè)列的含義,了解它不僅僅是要應(yīng)付面試,在實(shí)際開(kāi)發(fā)中也經(jīng)常會(huì)用到。比如對(duì)慢SQL
進(jìn)行分析,如果連執(zhí)行計(jì)劃結(jié)果都不會(huì)看,那還談什么SQL
優(yōu)化呢?
特別推薦一個(gè)分享架構(gòu)+算法的優(yōu)質(zhì)內(nèi)容,還沒(méi)關(guān)注的小伙伴,可以長(zhǎng)按關(guān)注一下:
長(zhǎng)按訂閱更多精彩▼
如有收獲,點(diǎn)個(gè)在看,誠(chéng)摯感謝
免責(zé)聲明:本文內(nèi)容由21ic獲得授權(quán)后發(fā)布,版權(quán)歸原作者所有,本平臺(tái)僅提供信息存儲(chǔ)服務(wù)。文章僅代表作者個(gè)人觀點(diǎn),不代表本平臺(tái)立場(chǎng),如有問(wèn)題,請(qǐng)聯(lián)系我們,謝謝!