Detailed explanation of mysql explain execution plan

 

1. The larger the number in the id column, the first it will be executed. If the number is the same, then it will be executed in order from top to bottom. If the id column is null, the table is a result set, and you do not need to use it to query.

 

2. The common select_type columns are:

A: simple: Indicates a simple select query that does not require union operations or does not contain subqueries. When there is a connection query, the outer query is simple, and there is only one

B: primary: a select that requires a union operation or contains a subquery, the select_type of the outermost unit query is primary. and only one

C: union: two select queries connected by union, the first query is derived derived table, except for the first table, the select_type of the second and subsequent tables is union

D: dependent union: Like union, it appears in the union or union all statement, but this query is affected by the outer query

E: union result: the result set containing union, in the union and union all statements, because it does not need to participate in the query, the id field is null

F: subquery: Except for the subquery contained in the from clause, the subquery appearing elsewhere may be a subquery

G: dependent subquery: Similar to dependent union, it means that the query of this subquery is affected by the query of the external table

H: derived: subqueries appearing in the from clause, also called derived tables, may be called inline views or nested selects in other databases

 

3、table

        The name of the query table displayed. If the query uses an alias, the alias is displayed here. If it does not involve operations on the data table, it is displayed as null. If it is displayed as <derived N> enclosed in angle brackets, it means this is Temporary table, followed by N is the id in the execution plan, indicating that the result comes from this query. If it is <union M,N> enclosed in angle brackets, similar to <derived N>, it is also a temporary table, indicating that the result comes from the result set with the id M,N of the union query.

 

4)、type

        In order from good to bad: system, const, eq_ref, ref, fulltext, ref_or_null, unique_subquery, index_subquery, range, index_merge, index, ALL, except all, other types can use indexes, except index_merge, other The type can only use one index

A: system: There is only one row of data in the table or an empty table, and it can only be used for myisam and memory tables. If it is an Innodb engine table, the type column is usually all or index in this case

B: const: When a unique index or primary key is used, and the returned record must be an equivalent where condition of one row record, the type is usually const. Other databases are also called unique index scans

C: eq_ref: Appears in the query plan to be connected to a table, the driver table returns only one row of data, and this row of data is the primary key or unique index of the second table, and must be not null, the unique index and the primary key are multiple column, eq_ref only occurs when all columns are used for comparison

D:ref: It does not require connection order like eq_ref, and there is no requirement for primary key and unique index, as long as it is retrieved using equality conditions, it may appear, and it is common to find equal values ​​with auxiliary indexes. Or in the multi-column primary key and unique index, the use of columns other than the first column as the equivalent search will also occur. In short, the equivalent search where the returned data is not unique may occur.

E: fulltext: full-text index retrieval. It should be noted that the priority of full-text index is very high. If full-text index and ordinary index exist at the same time, MySQL will prefer to use full-text index regardless of the cost.

F: ref_or_null: Similar to the ref method, except that the comparison of null values ​​is added. Not really used much.

G: unique_subquery: used for in-form subquery in where, the subquery returns unique values ​​with unique values

H: index_subquery: used for in-form sub-query using auxiliary index or in constant list, sub-query may return duplicate values, you can use index to deduplicate sub-query.

I: range: Index range scan, commonly used in queries using operators such as >,<,is null,between,in,like, etc.

J: index_merge: Indicates that the query uses more than two indexes, and finally takes the intersection or union. Common and and or conditions use different indexes. The official sorting is after ref_or_null, but in fact, all indexes need to be read. , performance may not be as good as range most of the time

K: index: Index full table scan, which scans the index from beginning to end. It is common to use index columns to process queries that do not need to read data files, and queries that can be sorted or grouped by index.

L:all: This is the full table scan data file, and then filter at the server layer to return the records that meet the requirements.

 

5、possible_keys

The indexes that may be used by the query will be listed here

 

6、key

Query the index actually used. When the select_type is index_merge, there may be more than two indexes here, and only one of the other select_types will appear here.

 

7 、 key_len

The length of the index used to process the query. If it is a single-column index, the entire index length is included. If it is a multi-column index, the query may not use all columns. Specifically, how many column indexes are used, here is the It will be counted, and the columns that are not used will not be counted here. Pay attention to the value of this column, and calculate the total length of your multi-column index to know if all the columns are used. It should be noted that indexes used by MySQL's ICP feature are not included. In addition, key_len only calculates the index length used by the where condition, and even if the index is used for sorting and grouping, it will not be calculated into key_len.

 

8、ref

If it is a constant equivalent query, const will be displayed here. If it is a join query, the execution plan of the driven table will display the associated fields of the driving table. If the condition uses an expression or function, or the condition column has an internal occurrence Implicit conversion, here may appear as func

 

9、rows

Here is the estimated number of scan lines in the execution plan, not the exact value

 

10、extra

This column can display a lot of information, there are dozens of them, the commonly used ones are

A: distinct: the distinct keyword is used in the select part

B: no tables used: query without from clause or From dual query

C: A join query that uses a subquery in the form of not in() or the not exists operator is called an anti-join. That is, the general join query is to query the inner table first, and then the outer table, and the anti-join is to query the outer table first, and then query the inner table.

D: using filesort: This occurs when the index cannot be used when sorting. Commonly used in order by and group by statements

E: using index: When querying, you do not need to return to the table to query, and you can get the queried data directly through the index.

F: using join buffer (block nested loop), using join buffer (batched key accss): Versions after 5.6.x optimize the BNL and BKA features of associated queries. The main purpose is to reduce the number of loops in the inner table and to scan queries sequentially.

G:using sort_union,using_union,using intersect,using sort_intersection:

using intersect: When the condition of each index of and is used, the information indicates that the intersection is obtained from the processing result

using union: Indicates that when using or to connect the conditions of each use index, this information indicates that the union is obtained from the processing result

using sort_union and using sort_intersection: Similar to the previous two, except that they appear when using and and or to query a large amount of information, first query the primary key, and then sort and merge before reading records and returning them.

H: using temporary: Indicates that a temporary table is used to store intermediate results. Temporary tables can be memory temporary tables and disk temporary tables, which cannot be seen in the execution plan. You need to check the status variable, used_tmp_table, and used_tmp_disk_table to see it.

I: using where: Indicates that not all the records returned by the storage engine meet the query conditions and need to be filtered at the server layer. The query conditions are divided into restriction conditions and check conditions. Before 5.6, the storage engine could only scan the data according to the restriction conditions and return it, and then the server layer filters the data according to the check conditions and returns the data that really matches the query. After 5.6.x, the ICP feature is supported, and the inspection conditions can also be pushed down to the storage engine layer. Data that does not meet the inspection conditions and restrictions will not be read directly, which greatly reduces the number of records scanned by the storage engine. extra column shows using index condition

J: firstmatch(tb_name): One of the new features of optimized subqueries introduced in 5.6.x, commonly used in subqueries whose where clause contains in() type. If the amount of data in the internal table is relatively large, this may occur

K: loosescan(m..n): One of the new features of optimized sub-queries introduced after 5.6.x. In the in() type sub-query, this may occur when there may be duplicate records returned by the sub-query

In addition to these, there are many query data dictionary libraries. During the execution plan process, it is found that some prompt information of impossible results is found.

 

11、filtered

This column will appear when explain extended is used. Versions after 5.7 have this field by default, so there is no need to use explain extended. This field indicates the proportion of the number of records that are left to satisfy the query after the data returned by the storage engine is filtered at the server layer. Note that it is a percentage, not a specific number of records.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325064783&siteId=291194637