mysql執行計划看是否最優


介紹

  本篇主要通過匯總網上的大牛的知識,簡單介紹一下如何使用mysql的執行計划,並根據執行計划判斷如何優化和是否索引最優。

  執行計划可顯示估計查詢語句執行計划,從中可以分析查詢的執行情況是否最優,有助於對不使用索引的語句進行優化。EXPLAIN對每個查詢返回一行信息,列出了有序的表格,MySQL處理語句的時候讀取他們。MySQL解決所有的連接使用嵌套連接方法。這意味讀取第一張一行,然后匹配第二張表的所有行,第三張表甚至更多表。當所有的表在處理時,MySQL會輸出已經查詢出來的列,並且回溯到表繼續查找直到所有的行被找到,從該表讀取下一行,直到程序繼續處理下一張表。

 
使用關鍵詞 EXTENDED ,EXPLAIN 會處理通過 SHOW WARNINGS 看到的一些額外信息。EXPLAIN EXTENDED 會顯示這些濾出的列。

語法:

EXPLAIN <select statement>;  

輸出表格字段如下:

    mysql> explain select * from mysql.user where user='root';  
    +----+-------------+-------+------+---------------+------+---------+------+------+-------------+  
    | id | select_type | table | type | possible_keys | key  | key_len | ref  | rows | Extra       |  
    +----+-------------+-------+------+---------------+------+---------+------+------+-------------+  
    |  1 | SIMPLE      | user  | ALL  | NULL          | NULL | NULL    | NULL |    4 | Using where |  
    +----+-------------+-------+------+---------------+------+---------+------+------+-------------+  

 

 

Column

JSONName

Meaning

id

select_id

查詢標識。id越大優先執行;id相同自上而下執行;

select_type

None

查詢的類型

table

table_name

查詢的表

partitions

partitions

Thematching partitions

type

access_type

連接類型

possible_keys

possible_keys

可能選擇的索引

key

key

實際使用的索引

key_len

key_length

使用的索引長度

ref

ref

哪一列或常數在查詢中與索引鍵列一起使用

rows

rows

估計查詢的行數

filtered

filtered

被條件過濾掉的行數百分比

Extra

None

解決查詢的一些額外信息

 

 

以下主要舉例說明3個字段:select_type 、type、Extra 

 

select_type

alue

JSONName

Meaning

SIMPLE

None

簡單查詢 (不使用UNION或子查詢)

PRIMARY

None

外層查詢,主查詢

UNION

None

UNION第二個語句或后面的語句

DEPENDENTUNION

dependent (true)

UNION中第二個語句或后面的語句,獨立於外部查詢

UNIONRESULT

union_result

UNION的結果

SUBQUERY

None

子查詢中第一個SELECT

DEPENDENTSUBQUERY

dependent (true)

子查詢中第一個SELECT,獨立於外部查詢

DERIVED

None

子查詢在 FROM子句中

MATERIALIZED

materialized_from_subquery

物化子查詢(不清楚是什么樣的查詢語句?)

UNCACHEABLESUBQUERY

cacheable (false)

結果集不能被緩存的子查詢,必須重新評估外層查詢的每一行

UNCACHEABLEUNION

cacheable (false)

UNION中第二個語句或后面的語句屬於不可緩存的子查詢

 

創建測試表:

create table tabname (  
id int auto_increment not null primary key,  
name varchar(10) null,  
indate datetime null,  
tid int null,  
key(tid),  
key(indate)  
)engine=innodb;  
  
  
create table tabname2 (  
id int auto_increment not null primary key,  
name varchar(10) null,  
indate datetime null,  
tid int null,  
key(tid),  
key(indate)  
)engine=myisam;  
  
  
insert into tabname(name,indate,tid) values('love',now(),2),('lucky',now(),3),('passion',now(),4);  
insert into tabname2(name,indate,tid) values('love',now(),2),('lucky',now(),3),('passion',now(),4); 

 

 



#SIMPLE

create table tabname (  
id int auto_increment not null primary key,  
name varchar(10) null,  
indate datetime null,  
tid int null,  
key(tid),  
key(indate)  
)engine=innodb;  
  
  
create table tabname2 (  
id int auto_increment not null primary key,  
name varchar(10) null,  
indate datetime null,  
tid int null,  
key(tid),  
key(indate)  
)engine=myisam;  
  
  
insert into tabname(name,indate,tid) values('love',now(),2),('lucky',now(),3),('passion',now(),4);  
insert into tabname2(name,indate,tid) values('love',now(),2),('lucky',now(),3),('passion',now(),4); 

 

 

#PRIMARY / DERIVED

    mysql> explain select * from (select * from tabname) as a;  
    +----+-------------+------------+------+---------------+------+---------+------+------+-------+  
    | id | select_type | table      | type | possible_keys | key  | key_len | ref  | rows | Extra |  
    +----+-------------+------------+------+---------------+------+---------+------+------+-------+  
    |  1 | PRIMARY     | <derived2> | ALL  | NULL          | NULL | NULL    | NULL |    3 |       |  
    |  2 | DERIVED     | tabname    | ALL  | NULL          | NULL | NULL    | NULL |    3 |       |  
    +----+-------------+------------+------+---------------+------+---------+------+------+-------+  

 

 

#PRIMARY / UNION / UNION RESULT

    mysql> explain select * from tabname union select * from tabname;  
    mysql> explain select * from tabname union all select * from tabname;  
    +----+--------------+------------+------+---------------+------+---------+------+------+-------+  
    | id | select_type  | table      | type | possible_keys | key  | key_len | ref  | rows | Extra |  
    +----+--------------+------------+------+---------------+------+---------+------+------+-------+  
    |  1 | PRIMARY      | tabname    | ALL  | NULL          | NULL | NULL    | NULL |    3 |       |  
    |  2 | UNION        | tabname    | ALL  | NULL          | NULL | NULL    | NULL |    3 |       |  
    | NULL | UNION RESULT | <union1,2> | ALL  | NULL          | NULL | NULL    | NULL | NULL |       |  
    +----+--------------+------------+------+---------------+------+---------+------+------+-------+  

 

    mysql> explain select * from tabname where id=(select max(id) from tabname);  
    +----+-------------+---------+-------+---------------+---------+---------+-------+------+------------------------------+  
    | id | select_type | table   | type  | possible_keys | key     | key_len | ref   | rows | Extra                        |  
    +----+-------------+---------+-------+---------------+---------+---------+-------+------+------------------------------+  
    |  1 | PRIMARY     | tabname | const | PRIMARY       | PRIMARY | 4       | const |    1 |                              |  
    |  2 | SUBQUERY    | NULL    | NULL  | NULL          | NULL    | NULL    | NULL  | NULL | Select tables optimized away |  
    +----+-------------+---------+-------+---------------+---------+---------+-------+------+------------------------------+  

 

#PRIMARY / SUBQUERY

 

 

#PRIMARY / DEPENDENT SUBQUERY

[plain] view plain copy
    mysql> explain select * from tabname a where exists(select 1 from tabname b where a.id=b.id);  
    mysql> explain select *,(select name from tabname b where a.id=b.id) from tabname a;  
    mysql> explain select * from tabname where id not in(select id from tabname);  
    +----+--------------------+---------+-----------------+---------------+---------+---------+------+------+-------------+  
    | id | select_type        | table   | type            | possible_keys | key     | key_len | ref  | rows | Extra       |  
    +----+--------------------+---------+-----------------+---------------+---------+---------+------+------+-------------+  
    |  1 | PRIMARY            | tabname | ALL             | NULL          | NULL    | NULL    | NULL |    3 | Using where |  
    |  2 | DEPENDENT SUBQUERY | tabname | unique_subquery | PRIMARY       | PRIMARY | 4       | func |    1 | Using index |  
    +----+--------------------+---------+-----------------+---------------+---------+---------+------+------+-------------+  

 

 

#PRIMARY / DEPENDENT UNION / DEPENDENT SUBQUERY / UNION RESULT

    mysql> explain select * from tabname where id in (select id from tabname union select id from tabname);  
    +----+--------------------+------------+--------+---------------+---------+---------+------+------+-------------+  
    | id | select_type        | table      | type   | possible_keys | key     | key_len | ref  | rows | Extra       |  
    +----+--------------------+------------+--------+---------------+---------+---------+------+------+-------------+  
    |  1 | PRIMARY            | tabname    | ALL    | NULL          | NULL    | NULL    | NULL |    3 | Using where |  
    |  2 | DEPENDENT SUBQUERY | tabname    | eq_ref | PRIMARY       | PRIMARY | 4       | func |    1 | Using index |  
    |  3 | DEPENDENT UNION    | tabname    | eq_ref | PRIMARY       | PRIMARY | 4       | func |    1 | Using index |  
    | NULL| UNION RESULT      | <union2,3> | ALL    | NULL          | NULL    | NULL    | NULL | NULL |             |  
    +----+--------------------+------------+--------+---------------+---------+---------+------+------+-------------+  

 

type

type

Meaning

system

表僅一行數據 (=system table).這是const連接類型的特例。

const

表最多只有一個匹配行,在查詢開始時被讀取。因為只有一個值,優化器將該列值視為常量。當在primarykey或者unique索引作為常量比較時被使用。

eq_ref(engine=myisam)

來自前面表的結果集中讀取一行,這是除systemconst外最好的連接類型。當在使用PRIMARYKEY或者UNIQUENOT NULL的索引時會被使用。

ref

對於前面表的結果集匹配查詢的所有行,當連接使用索引key時,或者索引不是PRIMARYKEYUNIQUE則使用該類型。如果使用索引匹配少量行時,是不錯的連接類型。

ref_or_null

連接類型類似ref,只是搜索的行中包含NULLMySQL做了額外的查找。

fulltext

使用全文索引時出現。

index_merge

使用了索引合並優化。(未成功)

unique_subquery

該類型將ref替換成以下子查詢的格式:

valueIN (SELECTprimary_key FROMsingle_table WHERE some_expr)

index_subquery

unique_subquery類似,但是將主鍵改為非唯一索引:

valueIN (SELECTkey_columnFROMsingle_table WHERE some_expr)

range

使用索引檢索給定范圍內的行。

index

該連接類型與ALL相同,除了掃描索引樹。如果查詢的字段都在索引列中,則使用index類型,否則為ALL類型。

ALL

對於前面表的結果集中,進行了全表掃描。最差的一種類型,應考慮查詢優化了!

 

查詢類型性能由優到差:

system > const > eq_ref > ref > fulltext > ref_or_null > index_merge > unique_subquery > index_subquery > range > index > ALL

 

#system

    mysql> explain select id from(select id from tabname where id=1) as a;  
    +----+-------------+------------+--------+---------------+---------+---------+------+------+-------------+  
    | id | select_type | table      | type   | possible_keys | key     | key_len | ref  | rows | Extra       |  
    +----+-------------+------------+--------+---------------+---------+---------+------+------+-------------+  
    |  1 | PRIMARY     | <derived2> | system | NULL          | NULL    | NULL    | NULL |    1 |             |  
    |  2 | DERIVED     | tabname    | const  | PRIMARY       | PRIMARY | 4       |      |    1 | Using index |  
    +----+-------------+------------+--------+---------------+---------+---------+------+------+-------------+  

 

#const

    mysql> explain select * from tabname as a,tabname as b where a.id=b.id and a.id=1;  
    mysql> explain select * from tabname where id=1;  
    +----+-------------+---------+-------+---------------+---------+---------+-------+------+-------+  
    | id | select_type | table   | type  | possible_keys | key     | key_len | ref   | rows | Extra |  
    +----+-------------+---------+-------+---------------+---------+---------+-------+------+-------+  
    |  1 | SIMPLE      | tabname | const | PRIMARY       | PRIMARY | 4       | const |    1 |       |  
    +----+-------------+---------+-------+---------------+---------+---------+-------+------+-------+  

 


#eq_ref(engine=myisam)

 

    mysql> explain select * from tabname2 as a,tabname2 as b where a.id=b.id;  
    +----+-------------+-------+--------+---------------+---------+---------+-----------+------+-------+  
    | id | select_type | table | type   | possible_keys | key     | key_len | ref       | rows | Extra |  
    +----+-------------+-------+--------+---------------+---------+---------+-----------+------+-------+  
    |  1 | SIMPLE      | a     | ALL    | PRIMARY       | NULL    | NULL    | NULL      |    3 |       |  
    |  1 | SIMPLE      | b     | eq_ref | PRIMARY       | PRIMARY | 4       | test.a.id |    1 |       |  
    +----+-------------+-------+--------+---------------+---------+---------+-----------+------+-------+  

 

 

 

 

#ref

    mysql> explain select * from tabname as a,tabname as b where a.tid=b.tid and a.tid=2;  
    mysql> explain select * from tabname where tid=2;  
    +----+-------------+---------+------+---------------+------+---------+-------+------+-------------+  
    | id | select_type | table   | type | possible_keys | key  | key_len | ref   | rows | Extra       |  
    +----+-------------+---------+------+---------------+------+---------+-------+------+-------------+  
    |  1 | SIMPLE      | tabname | ref  | tid           | tid  | 5       | const |    1 | Using where |  
    +----+-------------+---------+------+---------------+------+---------+-------+------+-------------+  

 

#ref_or_null

 

    mysql> explain select id,tid from tabname where tid=2 or tid is null;  
    +----+-------------+---------+-------------+---------------+------+---------+-------+------+--------------------------+  
    | id | select_type | table   | type        | possible_keys | key  | key_len | ref   | rows | Extra                    |  
    +----+-------------+---------+-------------+---------------+------+---------+-------+------+--------------------------+  
    |  1 | SIMPLE      | tabname | ref_or_null | tid           | tid  | 5       | const |    2 | Using where; Using index |  
    +----+-------------+---------+-------------+---------------+------+---------+-------+------+--------------------------+  

 

 

 

 

#fulltext

    mysql> explain select id,tid from tabname where tid=2 or tid is null;  
    +----+-------------+---------+-------------+---------------+------+---------+-------+------+--------------------------+  
    | id | select_type | table   | type        | possible_keys | key  | key_len | ref   | rows | Extra                    |  
    +----+-------------+---------+-------------+---------------+------+---------+-------+------+--------------------------+  
    |  1 | SIMPLE      | tabname | ref_or_null | tid           | tid  | 5       | const |    2 | Using where; Using index |  
    +----+-------------+---------+-------------+---------------+------+---------+-------+------+--------------------------+  

 

#index_merge(未成功)

mysql> explain select * from tabname where tid>1 or indate<now();  
mysql> explain select * from tabname where (tid>1 or indate>now()) AND name<'kk'; 

 


#unique_subquery

    mysql> explain select * from tabname where tid in(select id from tabname);  
    mysql> explain select * from tabname where id in(select id from tabname);  
    +----+--------------------+---------+-----------------+---------------+---------+---------+------+------+-------------+  
    | id | select_type        | table   | type            | possible_keys | key     | key_len | ref  | rows | Extra       |  
    +----+--------------------+---------+-----------------+---------------+---------+---------+------+------+-------------+  
    |  1 | PRIMARY            | tabname | ALL             | NULL          | NULL    | NULL    | NULL |    3 | Using where |  
    |  2 | DEPENDENT SUBQUERY | tabname | unique_subquery | PRIMARY       | PRIMARY | 4       | func |    1 | Using index |  
    +----+--------------------+---------+-----------------+---------------+---------+---------+------+------+-------------+  

 

#index_subquery

    mysql> explain select * from tabname where tid in(select tid from tabname);  
    mysql> explain select * from tabname where id in(select tid from tabname);  
    +----+--------------------+---------+----------------+---------------+------+---------+------+------+--------------------------+  
    | id | select_type        | table   | type           | possible_keys | key  | key_len | ref  | rows | Extra                    |  
    +----+--------------------+---------+----------------+---------------+------+---------+------+------+--------------------------+  
    |  1 | PRIMARY            | tabname | ALL            | NULL          | NULL | NULL    | NULL |    3 | Using where              |  
    |  2 | DEPENDENT SUBQUERY | tabname | index_subquery | tid           | tid  | 5       | func |    1 | Using index; Using where |  
    +----+--------------------+---------+----------------+---------------+------+---------+------+------+--------------------------+  

 

 

 

 

#range

 

    mysql> explain select * from tabname where tid between 1 and 2;  
    mysql> explain select * from tabname where id>1;  
    +----+-------------+---------+-------+---------------+---------+---------+------+------+-------------+  
    | id | select_type | table   | type  | possible_keys | key     | key_len | ref  | rows | Extra       |  
    +----+-------------+---------+-------+---------------+---------+---------+------+------+-------------+  
    |  1 | SIMPLE      | tabname | range | PRIMARY       | PRIMARY | 4       | NULL |    2 | Using where |  
    +----+-------------+---------+-------+---------------+---------+---------+------+------+-------------+  

 

 

 

#index

mysql> explain select id,tid from tabname;  
mysql> explain select tid from tabname;  
+----+-------------+---------+-------+---------------+------+---------+------+------+-------------+  
| id | select_type | table   | type  | possible_keys | key  | key_len | ref  | rows | Extra       |  
+----+-------------+---------+-------+---------------+------+---------+------+------+-------------+  
|  1 | SIMPLE      | tabname | index | NULL          | tid  | 5       | NULL |    3 | Using index |  
+----+-------------+---------+-------+---------------+------+---------+------+------+-------------+

 

#ALL

    mysql> explain select * from tabname where tid<>2;  
    mysql> explain select * from tabname;  
    +----+-------------+---------+------+---------------+------+---------+------+------+-------+  
    | id | select_type | table   | type | possible_keys | key  | key_len | ref  | rows | Extra |  
    +----+-------------+---------+------+---------------+------+---------+------+------+-------+  
    |  1 | SIMPLE      | tabname | ALL  | NULL          | NULL | NULL    | NULL |    3 |       |  
    +----+-------------+---------+------+---------------+------+---------+------+------+-------+  

 




Extra

該列輸出關MySQL如何解決查詢的額外信息。(下面列出部分常見的)

Extra

Meaning

usingwhere

使用過濾條件

usingindex

從索引樹中查找所有列

usingtemporary

使用臨時表存儲結果集,在使用groupbyorderby發生

selecttables optimized away

沒有groupby情況下使用min(),max(),或者count(*)

usingfilesort

有排序

notexists

leftjoin中匹配一行之后將不再繼續查詢查詢

distinct

查找到第一個匹配的行之后,MySQL則會停止對當前行的搜索

impossiblewhere

where子句總數失敗的查詢

impossiblehaving

having子句總數失敗的查詢

usingjoin buffer

使用連接緩存

Usingindex for group-by

Usingindex類似,在使用group-by時可從索引中找到字段


#using where

    mysql> explain select * from tabname where id>2;  
    mysql> explain select * from tabname where tid=2;  
    +----+-------------+---------+------+---------------+------+---------+-------+------+-------------+  
    | id | select_type | table   | type | possible_keys | key  | key_len | ref   | rows | Extra       |  
    +----+-------------+---------+------+---------------+------+---------+-------+------+-------------+  
    |  1 | SIMPLE      | tabname | ref  | tid           | tid  | 5       | const |    1 | Using where |  
    +----+-------------+---------+------+---------------+------+---------+-------+------+-------------+  


#using index

 

    mysql> explain select tid from tabname;  
    +----+-------------+---------+-------+---------------+------+---------+------+------+-------------+  
    | id | select_type | table   | type  | possible_keys | key  | key_len | ref  | rows | Extra       |  
    +----+-------------+---------+-------+---------------+------+---------+------+------+-------------+  
    |  1 | SIMPLE      | tabname | index | NULL          | tid  | 5       | NULL |    3 | Using index |  
    +----+-------------+---------+-------+---------------+------+---------+------+------+-------------+  

 

 

 

 

#using temporary

 
    mysql> explain select distinct name from tabname;  
    +----+-------------+---------+------+---------------+------+---------+------+------+-----------------+  
    | id | select_type | table   | type | possible_keys | key  | key_len | ref  | rows | Extra           |  
    +----+-------------+---------+------+---------------+------+---------+------+------+-----------------+  
    |  1 | SIMPLE      | tabname | ALL  | NULL          | NULL | NULL    | NULL |    3 | Using temporary |  
    +----+-------------+---------+------+---------------+------+---------+------+------+-----------------+  

 


#select tables optimized away

 

    mysql> explain select max(tid) from tabname;  
    +----+-------------+-------+------+---------------+------+---------+------+------+------------------------------+  
    | id | select_type | table | type | possible_keys | key  | key_len | ref  | rows | Extra                        |  
    +----+-------------+-------+------+---------------+------+---------+------+------+------------------------------+  
    |  1 | SIMPLE      | NULL  | NULL | NULL          | NULL | NULL    | NULL | NULL | Select tables optimized away |  
    +----+-------------+-------+------+---------------+------+---------+------+------+------------------------------+  

 

 

 

#using filesort

 

    mysql> explain select id,name from tabname group by id,name;  
    mysql> explain select * from tabname order by name;  
    +----+-------------+---------+------+---------------+------+---------+------+------+----------------+  
    | id | select_type | table   | type | possible_keys | key  | key_len | ref  | rows | Extra          |  
    +----+-------------+---------+------+---------------+------+---------+------+------+----------------+  
    |  1 | SIMPLE      | tabname | ALL  | NULL          | NULL | NULL    | NULL |    3 | Using filesort |  
    +----+-------------+---------+------+---------------+------+---------+------+------+----------------+  

 

 

 

 

#not exists

    mysql> explain select * from tabname a left join tabname b on a.id=b.id where b.id is null;  
    +----+-------------+-------+--------+---------------+---------+---------+-----------+------+-------------------------+  
    | id | select_type | table | type   | possible_keys | key     | key_len | ref       | rows | Extra                   |  
    +----+-------------+-------+--------+---------------+---------+---------+-----------+------+-------------------------+  
    |  1 | SIMPLE      | a     | ALL    | NULL          | NULL    | NULL    | NULL      |    3 |                         |  
    |  1 | SIMPLE      | b     | eq_ref | PRIMARY       | PRIMARY | 4       | test.a.id |    1 | Using where; Not exists |  
    +----+-------------+-------+--------+---------------+---------+---------+-----------+------+-------------------------+  

 

#distinct

mysql> explain select distinct a.id from tabname a left join tabname b on a.id=b.id;  
+----+-------------+-------+--------+---------------+---------+---------+-----------+------+------------------------------+  
| id | select_type | table | type   | possible_keys | key     | key_len | ref       | rows | Extra                        |  
+----+-------------+-------+--------+---------------+---------+---------+-----------+------+------------------------------+  
|  1 | SIMPLE      | a     | index  | NULL          | tid     | 5       | NULL      |    3 | Using index; Using temporary |  
|  1 | SIMPLE      | b     | eq_ref | PRIMARY       | PRIMARY | 4       | test.a.id |    1 | Using index; Distinct        |  
+----+-------------+-------+--------+---------------+---------+---------+-----------+------+------------------------------+ 

 

  1.  

#impossible where

    mysql> explain select * from tabname where 1=2;  
    +----+-------------+-------+------+---------------+------+---------+------+------+------------------+  
    | id | select_type | table | type | possible_keys | key  | key_len | ref  | rows | Extra            |  
    +----+-------------+-------+------+---------------+------+---------+------+------+------------------+  
    |  1 | SIMPLE      | NULL  | NULL | NULL          | NULL | NULL    | NULL | NULL | Impossible WHERE |  
    +----+-------------+-------+------+---------------+------+---------+------+------+------------------+  

 


#impossible having

mysql> explain select id,count(*) from tabname group by id having 1=2;  
mysql> explain select count(*) from tabname having 1=2;  
+----+-------------+-------+------+---------------+------+---------+------+------+-------------------+  
| id | select_type | table | type | possible_keys | key  | key_len | ref  | rows | Extra             |  
+----+-------------+-------+------+---------------+------+---------+------+------+-------------------+  
|  1 | SIMPLE      | NULL  | NULL | NULL          | NULL | NULL    | NULL | NULL | Impossible HAVING |  
+----+-------------+-------+------+---------------+------+---------+------+------+-------------------+

 

 

現在使用 EXTENDED 情況:

 

語法:

  1. EXPLAIN EXTENDED <select statement>;  


不使用 extended 和使用extended 的分析情況:

    mysql> explain select tid from tabname;  
    +----+-------------+---------+-------+---------------+------+---------+------+------+-------------+  
    | id | select_type | table   | type  | possible_keys | key  | key_len | ref  | rows | Extra       |  
    +----+-------------+---------+-------+---------------+------+---------+------+------+-------------+  
    |  1 | SIMPLE      | tabname | index | NULL          | tid  | 5       | NULL |    3 | Using index |  
    +----+-------------+---------+-------+---------------+------+---------+------+------+-------------+  
    1 row in set (0.00 sec)  
      
    mysql> explain extended select tid from tabname;  
    +----+-------------+---------+-------+---------------+------+---------+------+------+----------+-------------+  
    | id | select_type | table   | type  | possible_keys | key  | key_len | ref  | rows | filtered | Extra       |  
    +----+-------------+---------+-------+---------------+------+---------+------+------+----------+-------------+  
    |  1 | SIMPLE      | tabname | index | NULL          | tid  | 5       | NULL |    3 |   100.00 | Using index |  
    +----+-------------+---------+-------+---------------+------+---------+------+------+----------+-------------+  
    1 row in set, 1 warning (0.00 sec)  

 


可以看到,使用 extended 時,輸出的最下面多了 1 條警告。 此時可以用 show warnings 來查看:

    mysql> show warnings \G;  
    *************************** 1. row ***************************  
      Level: Note  
       Code: 1003  
    Message: select `test`.`tabname`.`tid` AS `tid` from `test`.`tabname`  
    1 row in set (0.00 sec)  
      
    ERROR:  
    No query specified  

 


show warnings 顯示了優化器中是怎么規范表和字段名的,在通過重寫和優化規則之后的 select 語句是什么樣子。

 

 

更多參考:

EXPLAIN Output Format

EXPLAIN EXTENDED Output Format


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM