天天看點

8.8.2 EXPLAIN Output Format 優化輸出格式

8.8.2 EXPLAIN Output Format 優化輸出格式

EXPLAIN 語句提供資訊關于執行計劃的資訊:

EXPLAIN 傳回一條記錄的資訊關于每個表用于SELECT 語句,

它列出了表的輸出順序 ,MySQL 會讀取當處理SQL語句的時候。

MySQL 解決所有的關聯使用一個嵌套循環算法,這意味着MySQL 從第一個表讀取一條記錄,

在第2個,第3個 表裡找到比對的記錄。

當所有的表被處理,MySQL 輸出選擇的列和回溯通過表清單直到一個表被找到有更多的比對的記錄。

下一行從這個表讀取,繼續處理下一個表。

當使用EXTENDED 關鍵字, EXPLAIN 産生額外的資訊可以通過SHOW WARNINGS 語句跟着EXPLAIN 語句查詢

. EXPLAIN EXTENDED 也可以顯示過濾的列,See Section 8.8.3, “EXPLAIN EXTENDED Output Format”.

注意:

你不能使用EXTENDED 和PARTITIONS 關鍵字結婚在同樣的EXPLAIN 語句。

EXPLAIN Output Columns EXPLAIN 輸出列

EXPLAIN Join Types EXPLAIN 關聯類型

EXPLAIN Extra Information EXPLAIN 額外的資訊

EXPLAIN Output Interpretation EXPLAIN 解釋輸出

EXPLAIN Output Columns EXPLAIN 輸出列

這個章節描述EXPLAIN 産生的輸出列, 稍後部分提供額外的資訊關于類型和額外的列

EXPLAIN 的每行輸出提供關于一個表的資訊, 每行包含值的總結在Table 8.1, “EXPLAIN Output Columns”,

并在表中更詳細的描述。列名在表的第一列顯示,第2列提供了等效的屬性名字。

Table 8.1 EXPLAIN Output Columns

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

mysql> explain SELECT cpi.personName, ccd.clientSn, ccd.income, ccd.pay, ccd.accountBalance, ccd.createdTime, ccd.remark from

-> (select * from ClientCashDetail ccd_int where

-> 1 >

-> (SELECT count(clientSn) from ClientCashDetail

-> where clientSn= ccd_int.clientSn and ccd_int.createdTime < createdTime and createdTime < TIMESTAMP(@dated_time) )

-> and ccd_int.createdTime < TIMESTAMP(@dated_time)

-> ) ccd

-> RIGHT JOIN ClientPersonalInfo cpi on cpi.clientSn = ccd.clientSn

-> where ccd.clientSn in (SELECT clientSn from ClientPersonalInfo where personName in (

-> ‘蔡明’,

-> ‘苑秀鳳’,

-> ))  
-> ORDER BY cpi.personName,  ccd.clientSn,  ccd.createdTime DESC;  
           

+—-+——————–+——————–+——–+—————+————-+———+——————-+——+———————————+

| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |

+—-+——————–+——————–+——–+—————+————-+———+——————-+——+———————————+

| 1 | PRIMARY | cpi | ALL | PRIMARY | NULL | NULL | NULL | 937 | Using temporary; Using filesort |

| 1 | PRIMARY | ClientPersonalInfo | eq_ref | PRIMARY | PRIMARY | 4 | zjzc.cpi.clientSn | 1 | Using where |

| 1 | PRIMARY | | ref | | | 4 | zjzc.cpi.clientSn | 10 | NULL |

| 2 | DERIVED | ccd_int | ALL | NULL | NULL | NULL | NULL | 5999 | Using where |

| 3 | DEPENDENT SUBQUERY | ClientCashDetail | ALL | NULL | NULL | NULL | NULL | 5999 | Using where |

+—-+——————–+——————–+——–+—————+————-+———+——————-+——+———————————+

5 rows in set (0.11 sec)

mysql> explain select userNick from Client where sn<1200;

+—-+————-+——–+——-+—————+———+———+——+——+————-+

| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |

+—-+————-+——–+——-+—————+———+———+——+——+————-+

| 1 | SIMPLE | Client | range | PRIMARY | PRIMARY | 4 | NULL | 1 | Using where |

+—-+————-+——–+——-+—————+———+———+——+——+————-+

1 row in set (0.00 sec)

id (JSON name: select_id)

SELECT 标示符, 這是SELECT 查詢的順序号。這個值可以是NULL,如果row隻想一個union結果集。

在這種情況下, 表列顯示值為