Turn: MYSQL [implementation plan]

 

What is the SQL execution plan

SQL execution plan, is a SQL statement, when in actual implementation of the database, step by step, respectively, have done. EXPLAIN that we use that information to show up when a SQL statement analysis

Importance of Learning

Learn SQL execution plan significance is that we can be more clear understanding by the implementation of a plan to this statement, which is divided into a few steps, there is no use the index, if there are some places that can be optimized and so on. The following items start a say.

All fields

EXPLAIN actually running a time, we can see below the header, let's that we take each item


 
Header field

id

select query sequence number, contains a set of numbers representing the order of execution of the query clauses or select operation table
three cases:
1, the same id: execution order from top to bottom


 
 

2, id different: If a subquery, id serial number is incremented, id value the greater the higher the priority, the first to be executed


 
 

. 3, the same but different id (two cases exist): id if the same, may be considered as a group, from the order of execution down; in all groups, the greater the id value, the higher priority, the first execution


 
 

select_type

The type of query, the query is mainly used to distinguish between normal, combined query, complex queries subqueries
1 SIMPLE: simply select query, the query does not include a subquery, or Union
2, PRIMARY: query contains any complex sub-portion, most outer query were labeled Primary
. 3, SUBQUERY: comprising subqueries where the select list or
4 DERIVED: from subquery included in the list are marked as derived (derivative), MySQL or recursive implementation of these sub-queries, the results zero in table
5 UNION: If after the second select appears in the union, were labeled union; union if the query from clause included in the sub, the outer layer will be marked as select derived
. 6, UNION RESULT: the union table select get results

 
 

 

type

Access Type, sql query optimization is a very important indicator of the value of the results from best to worst are:

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

In general, a good range of at least sql query level, preferably to ref

1, system: table has only one row (equal to the system tables), which is a special case of type const, usually does not appear to be negligible
2 const: represented by the index once found, const for comparing primary key or unique index. Because only one row of data matching, all quickly. If the primary key is placed where the list, mysql the query can be converted to a const

 
 

3, eq_ref : unique index scan, for each index key, the table is only one matching record. Common in the primary key or unique index scan.
 
 

NOTE: ALL table records the least full table scan table as t1 Table
4 ref : non-unique index scanning, returns all rows matching a separate value. Nature is also an index access, which returns all rows that match a single value, but he may find more qualified rows, so it should belong to a mixture of search and scan
 
 

5 range : retrieve only to the line given range, using an index to select the rows. key column shows the use of the index. General is bettween, <,>, in other queries appear in the where clause. This index scan range is better than a full column of index scan. Just need to start at some point and ends at another point, do not scan all indexes
 
 

6, index : Full Index Scan, index and index types to distinguish ALL traversing the index tree only. This is generally ALL blocks, should be smaller than the index file is generally a data file. (Index and ALL Though it is a full table read, but the index is read from the index, while ALL is read from the hard disk)
 
 

7, ALL : Full Scan the Table, traverse the whole table to find matching rows
 
 

 

possible_keys

There is an index on the field queries involved, then the index will be listed, but not necessarily the actual query

key

Index actually used, if NULL, then do not use the index.
If a query coverage index, the index appears only in the key list


 
 

 
 

key_len

Index indicates the number of bytes used, the length of the index used in the query (the maximum possible length), not the actual length, in theory, the length as short as possible. The table definition key_len is obtained by calculation, is not retrieved by the inner

ref

Column shows that the index is used, if possible, is a constant const.

rows

The selected index table statistics and case, a rough estimate of the number of lines needed to find the record to be read

Extra

Not suitable for display in other fields, but the additional information is very important
. 1, Using filesort:
MySQL use an external data ordering index, instead of reading sorted by index in the table. That mysql can not be used to complete the index sort operation become "Sort files"

 
 

Because the index is sorted first by email, then address the sort, so if a query by ordering direct address, the index can not meet the requirements, internal mysql must realize once again "Sort files"

 

2 Using temporary:
Use a temporary table to hold intermediate results, that uses mysql temporary table when sorting query results, common in order by and group by

 
 

 

3, Using index:
indicates that the corresponding select operation using the cover index (Covering Index), to avoid data access table rows, high efficiency
if there Using where at the same time, showed lookup index is used to perform the index key (refer to the FIG.)
If the useless appears Using where the same time, indicating that the index is used to read data rather than perform a lookup operation

 
 

 

Covering index (Covering Index): Also called index covering. Is to select fields in the list, you can only get from the index without reading the data file again according to the index, in other words a query column to be covered by the index built.
Note:
A, for the use of covering indexes, select fields in the list need only remove the column, do not use select *
b, if all fields are built index will lead to the index file is too large, but lower crud performance

4Using where :
Use a filter where

5Using join buffer :
Use the link cache

6、Impossible WHERE
where子句的值总是false,不能用来获取任何元祖

 
 

 

7、select tables optimized away
在没有group by子句的情况下,基于索引优化MIN/MAX操作或者对于MyISAM存储引擎优化COUNT(*)操作,不必等到执行阶段在进行计算,查询执行计划生成的阶段即可完成优化

8、distinct
优化distinct操作,在找到第一个匹配的元祖后即停止找同样值得动作

综合示例

 
 

执行顺序

  • 1(id = 4)、【select id, name from t2】:select_type 为union,说明id=4的select是union里面的第二个select。

  • 2(id = 3)、【select id, name from t1 where address = ‘11’】:因为是在from语句中包含的子查询所以被标记为DERIVED(衍生),where address = ‘11’ 通过复合索引idx_name_email_address就能检索到,所以type为index。

  • 3(id = 2)、【select id from t3】:因为是在select中包含的子查询所以被标记为SUBQUERY。

  • 4(id = 1)、【select d1.name, … d2 from … d1】:select_type为PRIMARY表示该查询为最外层查询,table列被标记为 “derived3”表示查询结果来自于一个衍生表(id = 3 的select结果)。

  • 5(id = NULL)、【 … union … 】:代表从union的临时表中读取行的阶段,table列的 “union 1, 4”表示用id=1 和 id=4 的select结果进行union操作。



Transfer: https://www.jianshu.com/p/514aa0b139d8

Guess you like

Origin www.cnblogs.com/fangyanr/p/11763485.html