共計 9258 個字符,預計需要花費 24 分鐘才能閱讀完成。
這篇文章主要講解了“mysql 執行計劃知識點有哪些”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著丸趣 TV 小編的思路慢慢深入,一起來研究和學習“mysql 執行計劃知識點有哪些”吧!
The DESCRIBE and EXPLAIN statements are synonyms, used either to obtain information about table structure or query execution plans.
DESCRIBE 和 EXPLAIN 語句是同義詞,用于獲得表結構信息和 SQL 語句的執行計劃。
The DESCRIBE and EXPLAIN statements are synonyms. In practice, the DESCRIBE keyword is more often used to obtain information about table structure, whereas EXPLAIN is used to obtain a query execution plan (that is, an explanation of how MySQL would execute a query). The following discussion uses the DESCRIBE and EXPLAIN keywords in accordance with those uses, but the MySQL parser treats them as completely synonymous.
DESCRIBE 和 EXPLAIN 語句是同義詞,實際上在平時使用過程中 DESCRIBE 多用于獲取表結構的信息,然后 EXPLAIN 多用于獲取 SQL 語句的執行計劃。MySQL 解析器對這兩個語句是完全作為同義詞對待的。
mysql desc mysql.plugin;
+——-+————–+——+—–+———+——-+
| Field | Type | Null | Key | Default | Extra |
+——-+————–+——+—–+———+——-+
| name | varchar(64) | NO | PRI | | |
| dl | varchar(128) | NO | | | |
+——-+————–+——+—–+———+——-+
2 rows in set (0.00 sec)
mysql explain mysql.plugin;
+——-+————–+——+—–+———+——-+
| Field | Type | Null | Key | Default | Extra |
+——-+————–+——+—–+———+——-+
| name | varchar(64) | NO | PRI | | |
| dl | varchar(128) | NO | | | |
+——-+————–+——+—–+———+——-+
2 rows in set (0.00 sec)
mysql desc select * from mysql.plugin;
+—-+————-+——–+——–+—————+——+———+——+——+———————+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+—-+————-+——–+——–+—————+——+———+——+——+———————+
| 1 | SIMPLE | plugin | system | NULL | NULL | NULL | NULL | 0 | const row not found |
+—-+————-+——–+——–+—————+——+———+——+——+———————+
1 row in set (0.07 sec)
mysql explain select * from mysql.plugin;
+—-+————-+——–+——–+—————+——+———+——+——+———————+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+—-+————-+——–+——–+—————+——+———+——+——+———————+
| 1 | SIMPLE | plugin | system | NULL | NULL | NULL | NULL | 0 | const row not found |
+—-+————-+——–+——–+—————+——+———+——+——+———————+
1 row in set (0.00 sec)
EXPLAIN 和 DESCRIBE 的語法(DESC 是 DESCRIBE 的縮寫)
{EXPLAIN | DESCRIBE | DESC}
tbl_name [col_name | wild]
{EXPLAIN | DESCRIBE | DESC}
[explain_type]
{explainable_stmt | FOR CONNECTION connection_id}
explain_type: {
EXTENDED
| PARTITIONS
| FORMAT = format_name
}
format_name: {
TRADITIONAL
| JSON
}
explainable_stmt: {
SELECT statement
| DELETE statement
| INSERT statement
| REPLACE statement
| UPDATE statement
}
1)EXPLAIN 和 DESCRIBE 同樣可以查看表字段
{EXPLAIN | DESCRIBE | DESC}
tbl_name [col_name | wild]
mysql desc mysql.plugin name;
+——-+————-+——+—–+———+——-+
| Field | Type | Null | Key | Default | Extra |
+——-+————-+——+—–+———+——-+
| name | varchar(64) | NO | PRI | | |
+——-+————-+——+—–+———+——-+
1 row in set (0.00 sec)
2)解析類型
{EXPLAIN | DESCRIBE | DESC}
[explain_type]
{explainable_stmt | FOR CONNECTION connection_id}
explain_type: {
EXTENDED
| PARTITIONS
| FORMAT = format_name
}
EXPLAIN EXTENDED:獲取執行計劃額外的信息
EXPLAIN PARTITIONS:是用于涉及到分區表的語句
EXPLAIN FORMAT
mysql EXPLAIN FORMAT=JSON select * from mysql.user where user= root
+————————————————————————————————————————————————————————————————————————————+
| EXPLAIN |
+————————————————————————————————————————————————————————————————————————————+
| {
query_block : {
select_id : 1,
table : {
table_name : user ,
access_type : ALL ,
rows : 6,
filtered : 100,
attached_condition : (`mysql`.`user`.`User` = root)
}
}
} |
+————————————————————————————————————————————————————————————————————————————+
1 row in set, 1 warning (0.01 sec)
mysql EXPLAIN FORMAT=TRADITIONAL 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 | 6 | Using where |
+—-+————-+——-+——+—————+——+———+——+——+————-+
1 row in set (0.00 sec)
如果不添加 FORMAT 默認為 TRADITIONAL
3)explainable_stmt
EXPLAIN 支持 SELECT DELETE INSERT REPLACE UPDATE 語句
EXPLAIN Output Columns(執行計劃輸出的列)
Column
JSON Name
Meaning
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(5.7)
filtered
Percentage of rows filtered by table condition
Extra
None
Additional information
id (JSON name: select_id)
執行計劃各個子任務的序號,這些序號是有序的。如果數據行指向其他行的聯合結果,該值可以為空,此時會顯示去說明指向的數據行。
select_type (JSON name: none)
執行計劃各個子任務的類型,下面是所有的類型
select_type Value
JSON Name
Meaning
SIMPLE
None
簡單查詢,不使用聯合查詢和子查詢
PRIMARY
None
最外層的查詢
UNION
None
聯合查詢中第二個或者后面的語句
DEPENDENT UNION
dependent (true)
聯合查詢中第二個或者后面的語句,取決于外面的查詢
UNION RESULT
union_result
聯合查詢的結果
SUBQUERY
None
子查詢中的第一個查詢
DEPENDENT SUBQUERY
dependent (true)
子查詢中的第一個查詢,取決于外面的查詢
DERIVED
None
FROM 后面的子查詢
MATERIALIZED
materialized_from_subquery
Materialized subquery
UNCACHEABLE SUBQUERY
cacheable (false)
A subquery for which the result cannot be cached and must be re-evaluated for each row of the outer query
UNCACHEABLE UNION
cacheable (false)
The second or later select in a UNION that belongs to an uncacheable subquery (see UNCACHEABLE SUBQUERY)
table (JSON name: table_name)
輸出行的表的名稱,也可以是下面的值
– : The row refers to the union of the rows with id values of M and N.
– : The row refers to the derived table result for the row with an id value of N. A derived table may result, for example, from a subquery in the FROM clause.
– : The row refers to the result of a materialized subquery for the row with an id value of N. See Section 9.2.2.2,“Optimizing Subqueries with Materialization”.
partitions (JSON name: partitions)
查詢匹配到的分區名稱,如果值為 NULL 說明沒有涉及分區表。
type (JSON name: access_type)
聯合 join 的類型,下面是各個類型:
system 連接系統表,表中只有一行數據
const 讀常量,且最多只會有一條數據,一般是使用主鍵或者唯一索引匹配常量(速度非常快)
eq_ref 最多只會有一條匹配結果,一般是通過主鍵或者唯一鍵索引來訪問或者連接(除 system、const 最快的連接)
ref Join 語句中被驅動表索引引用查詢
fulltext 使用 fulltext 索引
ref_or_null 和 ref 唯一區別是,多了 null 值查詢
index_merge 查詢中同時使用兩個(或更多)索引,然后對索引結果進行 merge 之后再讀取表數據
unique_subquery 子查詢中的返回結果字段組合是主鍵或者唯一約束
index_subquery 子查詢中的返回結果字段組合是一個索引(或索引組合),但不是一個主鍵或者唯一索引
range 索引范圍掃描
index 全索引掃描(1 覆蓋索引的全表查詢的情況,2 全表查詢,通過先查索引再查數據的情況)
ALL 全表掃描
possible_keys (JSON name: possible_keys)
possible_keys 列指出 MySQL 能使用哪個索引在該表中找到行。注意,該列完全獨立于 EXPLAIN 輸出所示的表的次序。這意味著在 possible_keys 中的某些鍵實際上不能按生成的表次序使用。
如果該列是 NULL,則沒有相關的索引。在這種情況下,可以通過檢查 WHERE 子句看是否它引用某些列或適合索引的列來提高你的查詢性能。如果是這樣,創造一個適當的索引并且再次用 EXPLAIN 檢查查詢
key (JSON name: key)
key 列顯示 MySQL 實際決定使用的鍵(索引)。如果沒有選擇索引,鍵是 NULL。要想強制 MySQL 使用或忽視 possible_keys 列中的索引,在查詢中使用 FORCE INDEX、USE INDEX 或者 IGNORE INDEX。
key_len (JSON name: key_length)
key_len 列顯示 MySQL 決定使用的鍵長度。如果鍵是 NULL,則長度為 NULL。
使用的索引的長度。在不損失精確性的情況下,長度越短越好
ref (JSON name: ref)
ref 列顯示使用哪個列或常數與 key 一起從表中選擇行。
如果該列的值為 func,說明存在額外信息,可以使用 SHOW WARNINGS 去查看。
rows (JSON name: rows)
MySQL 預估計的查詢需要執行的行數。
對于 InnoDB 表,該值不一定準確。
filtered (JSON name: filtered)(5.7)
預估的獲取的數據量在表中的百分比
Extra (JSON name: none)
這列包含了 MYSQL 如何處理語句的解決方案的額外信息。
Child of table pushed join@1
const row not found
Deleting all rows
Distinct
FirstMatch(tbl_name)
Full scan on NULL key
Impossible HAVING
Impossible WHERE
Impossible WHERE noticed after reading const tables
LooseScan(m..n)
No matching min/max row
no matching row in const table
No matching rows after partition pruning
No tables used
Not exists
Plan isn t ready yet
Range checked for each record
Scanned N databases
Select tables optimized away
Skip_open_table, Open_frm_only, Open_full_table
Start temporary, End temporary
unique row not found
Using filesort 當我們的 Query 中包含 ORDER BY 操作,而且無法利用索引完成排序操作的時候,MySQL Query Optimizer 不得不選擇相應的排序算法來實現
Using index 所需要的數據只需要在 Index 即可全部獲得而不需要再到表中取數據
Using index condition
Using index for group-by 數據訪問和 Using index 一樣,所需數據只需要讀取索引即可,而當 Query 中使用了 GROUPBY 或者 DISTINCT 子句的時候,如果分組字段也在索引中,Extra 中的信息就會是 Using index forgroup-by
Using join buffer (Block Nested Loop), Using join buffer (Batched Key Access)
Using MRR
Using sort_union(…), Using union(…), Using intersect(…)
Using temporary 當 MySQL 在某些操作中必須使用臨時表的時候,在 Extra 信息中就會出現 Using temporary。主要常見于 GROUP BY 和 ORDER BY 等操作中
Using where 如果我們不是讀取表的所有數據,或者不是僅僅通過索引就可以獲取所有需要的數據,則會出現 Using where 信息
Using where with pushed condition 這是一個僅僅在 NDBCluster 存儲引擎中才會出現的信息,而且還需要通過打開 ConditionPushdown 優化功能才可能會被使用。控制參數為 engine_condition_pushdown
Zero limit
感謝各位的閱讀,以上就是“mysql 執行計劃知識點有哪些”的內容了,經過本文的學習后,相信大家對 mysql 執行計劃知識點有哪些這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是丸趣 TV,丸趣 TV 小編將為大家推送更多相關知識點的文章,歡迎關注!