[MySQL] Detailed explanation of each field in the EXPLAIN statement

EXPLAIN Statement Overview

insert image description here
insert image description here
insert image description here

  • In the execution plan of the connection query : each table will correspond to a record, and the value of the id column of these records is the same;

  • In the execution plan containing subqueries : each select keyword will correspond to a unique id value.

  • Drive table: the table that appears in front;

  • Driven table: The table that appears after.

Detailed explanation of each field of EXPLAIN

1. The value of select_type is:

  1. PRIMARY

  2. UNION

  3. UNION RESULT : MySQL chooses to use temporary tables to complete the UNIONdeduplication of queries.

  4. SUBQUERY : If the query statement containing the subquery cannot be converted into the corresponding semi-join form, and the subquery is an irrelevant subquery , and the query optimizer decides to execute the subquery by materializing the subquery, then , selct_typewith a value of SUBQUERY.

  5. DEPENDENT SUBQUERY : If the query statement containing the subquery cannot be converted into the corresponding semi-join form, and the subquery is a correlated subquery , then selct_typethe value of is DEPENDENT SUBQUERY.

  6. DEPENDENT UNION

  7. DERIVED : In queries containing derived tables, execute the query as materialized derived tables.

  8. MATERIALIZED : When the query optimizer executes a statement containing a subquery, it chooses to materialize the subquery and connect it to the outer query, then the corresponding value of the subquery selct_typeisMATERIALIZED

  9. UNCACHEABLE SUBQUERY : not commonly used

  10. UNCACHEABLE UNION : not commonly used

2. type value (indicating the access method)

The complete access methods are: system, const, eq_ref, ref, fulltext, ref_or_null, index_merge, unique_subquery, index_subquery, range, index, ALL, etc.

  1. system : When there is only one record in the table, and the statistical data of the storage engine (for example, MyISAM, MEMORY) used by the table is accurate, then the access method of the table is system.

  2. const : When we perform equivalence matching with constants based on primary keys or unique secondary index columns, the access method for a single table is const. like:EXPLAIN SELECT * FROM s1 WHERE id = 5;

  3. eq_ref : If the driven table is accessed through the primary key or the only secondary index column that does not allow storage of NULL values ​​​​is a joint index , then all index columns must be compared for equality), then the access method of the driven table is eq_ref. For example, EXPLAIN SELECT * FROM s1 INNER JOIN s2 ON s1.id = s2.id;at this time, the type of s1 is ALL, and the type of s2 is eq_ref.

  4. ref : When a table is queried by means of equivalent matching between ordinary secondary index columns and constants, the access method of the table may be ref.

  5. fulltext : full text index

  6. ref_or_null : When equivalent matching is performed on ordinary secondary index columns, and the value of the index column can also be a NULL value, the access method of the table may be ref_or_null.

  7. index_merge : Normally, only scan intervals will be generated for a single index. Index merge.

  8. unique_subquery : Similar to the eq_ref access method of the driven table in the two-table join, unique_subquery is aimed at some query statements containing IN subqueries. If the query optimizer decides to convert an IN subquery to an EXISTS subquery, and after the conversion, the subquery can use the primary key or a unique secondary index that does not allow storage of NULL values ​​for equivalent matching, then the type column of the subquery The value of unique_subquery.

  9. index_subquery : Similar to unique_subquery, except that ordinary indexes are used when accessing the tables in the subquery.

  10. range :

  • Use the index to obtain the records of some single-point scanning intervals (for example, explain select * from s1 where key1 in ('a', 'b', 'c');, at this time, the value of the type column of table s1 is range).

  • It is used to obtain the records of a certain or certain range scanning interval (for example, explain select * from s1 where key1 > 'a' and key1 < 'b';, at this time, the value of the type column of table s1 is range).

  1. index:
  • Use index coverage, and need to scan all index records. For example, explain select key_part2 from s1 where key_part3 = 'a';, at this time, the value of the type column of table s1 is index.

    • Analysis: The key_part2 column and key_part3 column are included in the joint index idx_key_part, but the search condition key_part3='a' cannot form a suitable scan interval to reduce the number of records to be scanned, but can only scan the records of the entire idx_key_part index, so execute The value of the planned type column is index.
  • Special: For the InnoDB storage engine, when a full table scan is required and the primary key needs to be sorted, such as, , explain select * from s1 order by id;the value of the type column at this time is also index.

  1. ALL : Full table scan.

3. possible_keys 和 key

In the execution plan output by the EXPLAIN statement,

  • possible_keys column: Indicates which indexes may be used when executing a single-table query on a certain table in a certain query statement;

  • key column: Indicates which indexes are actually used. (After the query optimizer calculates the cost of using different indexes, it decides to use an index to execute the query)

4. key_len

The key_len value consists of the following 3 parts.

  1. The maximum length of storage space occupied by the actual data of this column.

  2. If the column can store NULL values, the value of key_len will add 1 byte on the basis of the maximum storage space length occupied by the actual data of the column.

  3. For a variable-length column, there will be 2 bytes of space to store the length of the storage space occupied by the actual data of the variable column, and the value of key_len will add 2 bytes to the original value.

5. ref

When the access method is one of const, eq_ref, ref, ref_or_null, unique_subquery, index_subquery, the ref column shows what is equivalent to the index column, for example, just a constant or a certain column.

6. rows

rows column:

  • Represents the estimated number of rows of the table (full table scan);

  • Represents the number of index rows expected to be scanned (execute queries using the index).

7. filtered

When analyzing the connection query cost, a concept of condition filtering (conditional filtering) was proposed. This concept is a strategy adopted by MySQL when calculating the fan-out of driving tables.

  • If you use a full table scan to execute a single table query, then when calculating the fan-out of the driving table, you need to estimate how many records satisfy all the search conditions.

  • If an index is used to perform a single-table scan, then when calculating the fan-out of the driving table, it is necessary to estimate how many records satisfy other search conditions besides the search conditions that form the index scan interval.

To give a small example, in the connection query, the rows column of the driving table x the filtered column of the driving table = the fan-out value of the driving table (how many queries should be performed on the driven table)

8. Extra

The Extra column is used to describe some additional information. There are many, the common ones are as follows:

  • No tables used : There is no FROM clause in the query statement.

  • Impossible WHERE : The WHERE clause is always FALSE.

  • No matching min/max row : When there is a MIN or MAX aggregate function in the query list, but does not meet the search conditions in the WHERE clause, this additional information will be prompted.

  • Using Index : Execute the query using a covering index.

  • Using index condition : Although index columns appear in some search conditions, they cannot be used as boundary conditions to form scanning intervals, that is, they cannot be used to reduce the number of records that need to be scanned.

  • Using where : A certain search condition needs to be judged at the server layer.

  • Using filesort : Execute the query using filesort. ( file sorting : the way to sort in memory or on disk)

  • Using temporary : Create an internal temporary table to execute the query.

Easter eggs - Extented EXPLAIN

After using the EXPLAIN statement to view the execution plan of a query, you can also use the SHOW WARNINGS statement to view the extended information related to the execution plan of the query.
insert image description here

Guess you like

Origin blog.csdn.net/xiaoyue_/article/details/130575636