mysql執(zhí)行計劃知識點有哪些

這篇文章主要講解了“MySQL執(zhí)行計劃知識點有哪些”,文中的講解內(nèi)容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“mysql執(zhí)行計劃知識點有哪些”吧!

創(chuàng)新互聯(lián)專注于多倫企業(yè)網(wǎng)站建設,成都響應式網(wǎng)站建設,成都商城網(wǎng)站開發(fā)。多倫網(wǎng)站建設公司,為多倫等地區(qū)提供建站服務。全流程定制網(wǎng)站建設,專業(yè)設計,全程項目跟蹤,創(chuàng)新互聯(lián)專業(yè)和態(tài)度為您提供的服務

The DESCRIBE and EXPLAIN statements are synonyms, used either to obtain information about table structure or query execution plans.

DESCRIBE和EXPLAIN語句是同義詞,用于獲得表結構信息和SQL語句的執(zhí)行計劃。

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語句的執(zhí)行計劃。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:獲取執(zhí)行計劃額外的信息

EXPLAIN PARTITIONS :是用于涉及到分區(qū)表的語句


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(執(zhí)行計劃輸出的列)


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(5.7) filtered Percentage of rows filtered by table condition
Extra None Additional information

id (JSON name: select_id)

執(zhí)行計劃各個子任務的序號,這些序號是有序的。如果數(shù)據(jù)行指向其他行的聯(lián)合結果,該值可以為空,此時會顯示去說明指向的數(shù)據(jù)行。

select_type (JSON name: none)

執(zhí)行計劃各個子任務的類型,下面是所有的類型

select_type Value JSON Name Meaning
SIMPLE

None

簡單查詢,不使用聯(lián)合查詢和子查詢

PRIMARY None 最外層的查詢
UNION None 聯(lián)合查詢中第二個或者后面的語句
DEPENDENT UNION

dependent (true)

聯(lián)合查詢中第二個或者后面的語句,取決于外面的查詢

UNION RESULT union_result 聯(lián)合查詢的結果
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)

查詢匹配到的分區(qū)名稱,如果值為NULL說明沒有涉及分區(qū)表。

type (JSON name: access_type)

聯(lián)合join的類型,下面是各個類型:

system                     連接系統(tǒng)表,表中只有一行數(shù)據(jù)

const                        讀常量,且最多只會有一條數(shù)據(jù),一般是使用主鍵或者唯一索引匹配常量(速度非??欤?/p>

eq_ref                       最多只會有一條匹配結果,一般是通過主鍵或者唯一鍵索引來訪問或者連接(除system、const最快的連接)

ref                             Join 語句中被驅動表索引引用查詢

fulltext                       使用fulltext索引

ref_or_null                  和ref唯一區(qū)別是,多了null值查詢

index_merge               查詢中同時使用兩個(或更多)索引,然后對索引結果進行merge 之后再讀取表數(shù)據(jù)

unique_subquery         子查詢中的返回結果字段組合是主鍵或者唯一約束

index_subquery          子查詢中的返回結果字段組合是一個索引(或索引組合),但不是一個主鍵或者唯一索引

range                         索引范圍掃描

index                         全索引掃描(1覆蓋索引的全表查詢的情況,2全表查詢,通過先查索引再查數(shù)據(jù)的情況)

ALL                              全表掃描

possible_keys (JSON name: possible_keys)

possible_keys列指出MySQL能使用哪個索引在該表中找到行。注意,該列完全獨立于EXPLAIN輸出所示的表的次序。這意味著在possible_keys中的某些鍵實際上不能按生成的表次序使用。

如果該列是NULL,則沒有相關的索引。在這種情況下,可以通過檢查WHERE子句看是否它引用某些列或適合索引的列來提高你的查詢性能。如果是這樣,創(chuàng)造一個適當?shù)乃饕⑶以俅斡肊XPLAIN檢查查詢

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列顯示使用哪個列或常數(shù)與key一起從表中選擇行。

如果該列的值為func,說明存在額外信息,可以使用SHOW WARNINGS去查看。

rows (JSON name: rows)

MySQL預估計的查詢需要執(zhí)行的行數(shù)。

對于InnoDB表,該值不一定準確。

filtered (JSON name: filtered)(5.7)

預估的獲取的數(shù)據(jù)量在表中的百分比

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 不得不選擇相應的排序算法來實現(xiàn)

Using index     所需要的數(shù)據(jù)只需要在Index 即可全部獲得而不需要再到表中取數(shù)據(jù)

Using index condition

Using index for group-by   數(shù)據(jù)訪問和Using index 一樣,所需數(shù)據(jù)只需要讀取索引即可,而當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 信息中就會出現(xiàn)Using temporary 。主要常見于GROUP BY 和ORDER BY 等操作中

Using where     如果我們不是讀取表的所有數(shù)據(jù),或者不是僅僅通過索引就可以獲取所有需要的數(shù)據(jù),則會出現(xiàn)Using where 信息

Using where with pushed condition 這是一個僅僅在NDBCluster 存儲引擎中才會出現(xiàn)的信息,而且還需要通過打開ConditionPushdown 優(yōu)化功能才可能會被使用??刂茀?shù)為engine_condition_pushdown

Zero limit

感謝各位的閱讀,以上就是“mysql執(zhí)行計劃知識點有哪些”的內(nèi)容了,經(jīng)過本文的學習后,相信大家對mysql執(zhí)行計劃知識點有哪些這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關知識點的文章,歡迎關注!

文章名稱:mysql執(zhí)行計劃知識點有哪些
新聞來源:http://muchs.cn/article38/piessp.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供標簽優(yōu)化、軟件開發(fā)App設計、搜索引擎優(yōu)化Google、小程序開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站建設