昨天中午在食堂,和部門的技術大牛們坐在一桌吃飯,做爲一個卑微技術渣仔默默的吃着飯,聽大佬們高談闊論,研究各類高端技術,我TM也想說話可實在插不上嘴。html
聊着聊着忽然說到他上午面試了一個工做6年的程序員,表情挺複雜,他說:我看他簡歷寫着熟悉SQL
語句調優,就問了下 Explain
執行計劃怎麼看?結果這老哥一問三不知,工做6年這麼基礎的東西都不瞭解!mysql
感覺到了大佬的王之鄙視,回到工位我就開始默默寫這個,哎~ 我TM也不太懂 Explain
,老哥你這是針對我啊!哭唧唧~
程序員
當Explain
與 SQL
語句一塊兒使用時,MySQL
會顯示來自優化器關於SQL執行的信息。也就是說,MySQL
解釋了它將如何處理該語句,包括如何鏈接表以及什麼順序鏈接表等。面試
sql
的查詢類型Explain
執行計劃包含字段信息以下:分別是 id
、select_type
、table
、partitions
、type
、possible_keys
、key
、key_len
、ref
、rows
、filtered
、Extra
12個字段。
下邊咱們會結合具體的SQL
示例,詳細的解讀每一個字段以及每一個字段中不一樣參數的含義,如下全部示例數據庫版本爲 MySQL.5.7.17
。sql
mysql> select version() from dual; +------------+ | version() | +------------+ | 5.7.17-log | +------------+
咱們建立三張表 one
、two
、three
,表之間的關係 one.two_id = two.two_id AND two.three_id = three.three_id
。數據庫
id:
:表示查詢中執行select子句或者操做表的順序,id
的值越大,表明優先級越高,越先執行。 id
大體會出現 3種狀況:函數
id
相同看到三條記錄的id
都相同,能夠理解成這三個表爲一組,具備一樣的優先級,執行順序由上而下,具體順序由優化器決定。oop
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 | +----+-------------+-------+------------+--------+---------------+---------+---------+----------------------+------+----------+----------------------------------------------------+
id
不一樣若是咱們的 SQL
中存在子查詢,那麼 id
的序號會遞增,id
值越大優先級越高,越先被執行 。當三個表依次嵌套,發現最裏層的子查詢 id
最大,最早執行。性能
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 | +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+
將上邊的 SQL
稍微修改一下,增長一個子查詢,發現 id
的以上兩種同時存在。相同id
劃分爲一組,這樣就有三個組,同組的從上往下順序執行,不一樣組 id
值越大,優先級越高,越先執行。學習
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
查詢的類型,主要是用於區分各類複雜的查詢,例如:普通查詢
、聯合查詢
、子查詢
等。
SIMPLE
:表示最簡單的 select 查詢語句,也就是在查詢中不包含子查詢或者 union
交併差集等操做。
PRIMARY
:當查詢語句中包含任何複雜的子部分,最外層查詢則被標記爲PRIMARY
。
SUBQUERY
:當 select
或 where
列表中包含了子查詢,該子查詢被標記爲:SUBQUERY
。
DERIVED
:表示包含在from
子句中的子查詢的select,在咱們的 from
列表中包含的子查詢會被標記爲derived
。
UNION
:若是union
後邊又出現的select
語句,則會被標記爲union
;若 union
包含在 from
子句的子查詢中,外層 select
將被標記爲 derived
。
UNION RESULT
:表明從union
的臨時表中讀取數據,而table
列的<union1,4>
表示用第一個和第四個select
的結果進行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 | +------+--------------+------------+------------+-------+---------------+---------+---------+------+------+----------+-----------------+
查詢的表名,並不必定是真實存在的表,有別名顯示別名,也可能爲臨時表,例如上邊的DERIVED
、 <union1,4>
等。
查詢時匹配到的分區信息,對於非分區表值爲NULL
,當查詢的是分區表時,partitions
顯示分區表命中的分區狀況。
+----+-------------+----------------+---------------------------------+-------+---------------+---------+---------+------+------+----------+-------------+ | 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
:查詢使用了何種類型,它在 SQL
優化中是一個很是重要的指標,如下性能從好到壞依次是:system
> const
> eq_ref
> ref
> ref_or_null
> index_merge
> unique_subquery
> index_subquery
> range
> index
> ALL
system
: 當表僅有一行記錄時(系統表),數據量不多,每每不須要進行磁盤IO,速度很是快。
const
:表示查詢時命中 primary key
主鍵或者 unique
惟一索引,或者被鏈接的部分是一個常量(const
)值。這類掃描效率極高,返回數據量少,速度很是快。
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 | +----+-------------+-------+------------+-------+---------------+---------+---------+-------+------+----------+-------+
eq_ref
:查詢時命中主鍵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 | +----+-------------+-------+------------+--------+---------------+----------+---------+--------------------+------+----------+-------------+
ref
:區別於eq_ref
,ref
表示使用非惟一性索引,會找到不少個符合條件的行。
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 | +----+-------------+-------+------------+------+---------------+----------+---------+-------+------+----------+-------------+
ref_or_null
:這種鏈接類型相似於 ref,區別在於 MySQL
會額外搜索包含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 | +----+-------------+-------+------------+-------------+---------------+----------+---------+-------+------+----------+--------------------------+
index_merge
:使用了索引合併優化方法,查詢使用了兩個以上的索引。
下邊示例中同時使用到主鍵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 | +----+-------------+-------+------------+-------------+------------------+------------------+---------+------+------+----------+------------------------------------------------+
unique_subquery
:替換下面的 IN
子查詢,子查詢返回不重複的集合。
value IN (SELECT primary_key FROM single_table WHERE some_expr)
index_subquery
:區別於unique_subquery
,用於非惟一索引,能夠返回重複值。
value IN (SELECT key_column FROM single_table WHERE some_expr)
range
:使用索引選擇行,僅檢索給定範圍內的行。簡單點說就是針對一個有索引的字段,給定範圍檢索數據。在where
語句中使用 bettween...and
、<
、>
、<=
、in
等條件查詢 type
都是 range
。
舉個栗子: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 | +----+-------------+-------+------------+-------+---------------+---------+---------+------+------+----------+-------------+
從結果中看到只有對設置了索引的字段,作範圍檢索 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 | +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+
index
:Index
與ALL
其實都是讀全表,區別在於index
是遍歷索引樹讀取,而ALL
是從硬盤中讀取。
下邊示例:three_id
爲主鍵,不帶 where
條件全表查詢 ,type
結果爲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 | +----+-------------+-------+------------+-------+---------------+---------+---------+------+------+----------+-------------+
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
:表示在MySQL
中經過哪些索引,能讓咱們在表中找到想要的記錄,一旦查詢涉及到的某個字段上存在索引,則索引將被列出,但這個索引並不定一會是最終查詢數據時所被用到的索引。具體請參考上邊的例子。
key
:區別於possible_keys
,key是查詢中實際使用到的索引,若沒有使用索引,顯示爲NULL
。具體請參考上邊的例子。
當
type
爲index_merge
時,可能會顯示多個索引。
key_len
:表示查詢用到的索引長度(字節數),原則上長度越短越好 。
注意:
key_len
只計算where
條件中用到的索引長度,而排序和分組即使是用到了索引,也不會計算到key_len
中。
ref
:常見的有:const
,func
,null
,字段名。
const
,關聯字段
表達式
、函數
,或者條件列發生內部隱式轉換,可能顯示爲func
null
rows
:以表的統計信息和索引使用狀況,估算要找到咱們所需的記錄,須要讀取的行數。
這是評估SQL
性能的一個比較重要的數據,mysql
須要掃描的行數,很直觀的顯示 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
這個是一個百分比的值,表裏符合條件的記錄數的百分比。簡單點說,這個字段表示存儲引擎返回的數據在通過過濾後,剩下知足條件的記錄數量的比例。
在MySQL.5.7
版本之前想要顯示filtered
須要使用explain extended
命令。MySQL.5.7
後,默認explain
直接顯示partitions
和filtered
的信息。
Extra
:不適合在其餘列中顯示的信息,Explain
中的不少額外的信息會在 Extra
字段顯示。
Using index
:咱們在相應的 select
操做中使用了覆蓋索引,通俗一點講就是查詢的列被索引覆蓋,使用到覆蓋索引查詢速度會很是快,SQl
優化中理想的狀態。
什麼又是覆蓋索引?
一條 SQL
只須要經過索引就能夠返回,咱們所須要查詢的數據(一個或幾個字段),而沒必要經過二級索引,查到主鍵以後再經過主鍵查詢整行數據(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 | +----+-------------+-------+------------+-------+---------------+------------+---------+------+------+----------+-------------+
注意:想要使用到覆蓋索引,咱們在 select
時只取出須要的字段,不可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 | +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------+
Using where
:查詢時未找到可用的索引,進而經過where
條件過濾獲取所需數據,但要注意的是並非全部帶where
語句的查詢都會顯示Using where
。
下邊示例create_time
並未用到索引,type
爲 ALL
,即MySQL
經過全表掃描後再按where
條件篩選數據。
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 | +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+
Using temporary
:表示查詢後結果須要使用臨時表來存儲,通常在排序或者分組查詢時用到。
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 | +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+
Using filesort
:表示沒法利用索引完成的排序操做,也就是ORDER BY
的字段沒有索引,一般這樣的SQL都是須要優化的。
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
字段有索引就會用到覆蓋索引,相比執行速度快不少。
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 | +----+-------------+-------+------------+-------+---------------+---------+---------+------+------+----------+-------------+
Using join buffer
:在咱們聯表查詢的時候,若是表的鏈接條件沒有用到索引,須要有一個鏈接緩衝區來存儲中間結果。
先看一下有索引的狀況:鏈接條件 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 | +----+-------------+-------+------------+-------+---------------+----------+---------+----------------------+------+----------+--------------------------+
接下來刪掉 鏈接條件 one_name
、two_name
的字段索引。發現Extra
列變成 Using join buffer
,type
均爲全表掃描,這也是SQL
優化中須要注意的地方。
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) | +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+----------------------------------------------------+
Impossible where
:表示在咱們用不太正確的where
語句,致使沒有符合條件的行。
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 | +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+------------------+
No tables used
:咱們的查詢語句中沒有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
列的信息很是很是多,這裏就再也不一一列舉了,詳見 MySQL
官方文檔 :https://dev.mysql.com/doc/refman/5.7/en/explain-output.html#jointype_index_merge
上邊只是簡單介紹了下 Explain
執行計劃各個列的含義,瞭解它不只僅是要應付面試,在實際開發中也常常會用到。好比對慢SQL
進行分析,若是連執行計劃結果都不會看,那還談什麼SQL
優化呢?
整理了幾百本各種技術電子書和視頻課程 ,噓~,「免費」 送給小夥伴們。關注公號回覆【666】自行領取。和一些小夥伴們建了一個技術交流羣,一塊兒探討技術、分享技術資料,旨在共同窗習進步,若是感興趣就掃碼加入咱們吧!