Detailed explanation of MySQL query plan

Published 2026-07-30 20:18 Updated 2026-10-13 17:14 1501 words 8 min read ... Page views AI-reviewed An AI proof-read this post, but it still deserves a careful read. AI-rewritten Someone else wrote the original; AI helped me rewrite and polish it.

AI-reviewed An AI proof-read this post, but it still deserves a careful read.

This article details the use of EXPLAIN query plans in MySQL and the meaning of key fields. Using EXPLAIN, you can view the execution plan of SQL statements, including information such as access order, index selection, number of rows scanned, and whether sorting or temporary tables are needed to help analyze query performance. The article focuses on explaining the meaning of columns such as id, select_type, type, possible_keys, key_len, rows, filtered and Extra, and provides systematic ideas for analyzing execution plans. It is recommended to combine EXPLAIN ANALYZE to obtain actual execution data to verify the optimization effect.

MySQL EXPLAIN Detailed explanation of query plan

EXPLAIN Used to view the execution plan generated by MySQL for a SQL statement. Through execution plans, information such as access order, index selection, estimated scan rows, sorting, and temporary tables can be analyzed.

DESC with EXPLAIN the difference between

View table structure

DESC is DESCRIBE The abbreviation for, used to view field information in a table or view.

DESC employee;

DESCRIBE employee;

These two statements usually display the field name, data type, and whether the field name is allowed NULL, index type, default values, and additional information.

View SQL execution plan

Add before query statement EXPLAIN, you can view the execution plan selected by the optimizer.

EXPLAIN
SELECT *
FROM employee
WHERE first_name = 'tom';
image-001
image-001

MySQL also supports other output formats. For example, the tree format makes it easier to observe the execution order:

EXPLAIN FORMAT=TREE
SELECT *
FROM employee
WHERE first_name = 'tom';

EXPLAIN ANALYZE SQL is actually executed and estimates and actual execution data are returned. Update or delete statements should be used with caution to avoid accidentally modifying data.

EXPLAIN ANALYZE
SELECT *
FROM employee
WHERE first_name = 'tom';

EXPLAIN common column

image-002
image-002

Traditional table formats usually contain the following columns:

listingrole
idQuery block number, which can help determine the execution relationship between query blocks
select_typeThe type of query block, such as simple query, main query, subquery, or derived table
tableTable, alias, or internal temporary result currently accessed
partitionsPartition expected to be accessed; usually when partition tables are not used NULL
typeTable access method is an important indicator to determine index usage
possible_keysIndex that the optimizer thinks is likely to use
keyActual selected index
key_lenExpected index key length in bytes
refConstant or column to compare to indexed column
rowsThe optimizer estimates the number of rows to check
filteredPercentage expected to retain after filtering through current table conditions
ExtraSupplementary information such as sorting, temporary tables, and overlay indexes

id: Query block number

id Used to identify different query blocks, but cannot simply be regarded as an absolute execution order.

  • id The same rows usually belong to the same query block, and the order in which the tables are displayed reflects the order of access in the join plan.
  • id Different usually indicates the presence of a subquery, derived table, or UNION Wait for multiple query blocks.
  • Certain results are consistent id may be NULL, for example UNION RESULT。

The real execution process of complex SQL should also be combined FORMAT=TREE or EXPLAIN ANALYZE Judgment.

select_type: Query type

Common values are as follows:

valuemeaning
SIMPLEnot contain UNION or subquery
PRIMARYoutermost query
UNIONUNION The second and subsequent query blocks in
DEPENDENT UNIONDependent on outer query results UNION query block
UNION RESULTUNION result set
SUBQUERYSubqueries that do not rely on outer queries
DEPENDENT SUBQUERYRelated subqueries that rely on outer query results
DERIVEDFROM Derived table in clause
MATERIALIZEDSubquery results that are materialized and reused

table: Access objects

table Generally displays table names or table aliases. For the internal results generated by the optimizer, the following forms may occur:

  • <derivedN>: From the number numbered N The derived table query block of.
  • <unionM,N>: From the number numbered M、N Wait for query block UNION result.
  • <subqueryN>: The materialized subquery result.

type: Access method

type Describes how MySQL reads data from tables. Common types are listed below in order from precise to broad, but actual performance is also affected by factors such as return rows, data distribution, and caching.

typedescription
systemTable is treated as having only one row, yes const a special case of
constReturns up to one row through equivalent matching of the primary key or unique index
eq_refWhen joining, match is made through the primary key or non-empty unique index, and each previous table combination hits at most one row
refEquivalent lookup through incomplete prefixes of a common index or unique index may return multiple rows
fulltextUse full-text indexing
ref_or_nullsimilar ref, while looking for additional NULL
index_mergeMerge scan results from multiple indexes
unique_subquerycertain IN Subquery looks up through a unique index
index_subquerycertain IN Subquery looks up through non-unique index
rangePerforms a range scan on the index
indexScan the entire index
ALLScan the entire table

Analysis usually requires focused attention ALL and index。However, scanning small and full tables is not necessarily a problem, and cannot be based on type Determine individually whether SQL needs optimization.

index dependent column

possible_keys

Displays the indexes that the optimizer believes are likely to use. value is NULL Indicates that there is no obvious index available for the current query, but this does not mean that SQL must have a performance problem.

key

Displays the index that the optimizer finally selected. even possible_keys Multiple indexes are listed,key Usually only the index actually used is displayed; using index_merge Multiple indexes may be displayed when.

key_len

Displays the length of the index key that the execution plan is expected to use. For federated indexes, this value can be used to help determine which index columns are used.

key_len is the estimated maximum length and does not represent the actual data length read. Character sets, nullable columns, variable-length fields, and indexed column types all affect this value.

ref

Displays the values used for comparison during index lookup. For example:

  • const: Compare with constants.
  • 库名.表名.列名: Compare with columns in another table.
  • func: Compare with an expression, function result, or value to which a type conversion occurred.

rows with filtered

rows

rows It is the optimizer that estimates the number of rows to be checked, not the actual number of rows scanned. When statistical information is inaccurate, the estimation results may also deviate.

filtered

filtered is the proportion of data expected to be retained after filtering through the current table conditions, in percentage.

You can roughly estimate the number of rows passed to the next step in the following way:

rows × filtered ÷ 100

If you need to observe the actual execution rows and time spent, you should use the EXPLAIN ANALYZE。

Extra: Supplementary information

Extra Common information in:

informationdescription
Using indexUse the override index to get the columns you need, usually without going back to the table
Using whereAfter reading the data, it also needs to be filtered based on conditions
Using index conditionUse index conditions to push down and filter some records at the storage engine level
Using filesortSorting cannot directly utilize the appropriate index and requires additional sorting; you don’t have to actually write disk files
Using temporaryUse internal temporary tables to hold intermediate results, commonly used in partial grouping, deduplication, or sorting operations
Using join bufferThe connection process uses connection buffers and usually requires checking connection conditions and indexes
FirstMatchOne of the semi-connection optimization strategies, stop searching after finding the first match
LooseScanOne of the semi-connection optimization strategies is to reduce duplicate matches through index skip scanning
Impossible WHEREThe optimizer determines that the query condition cannot be established

Extra Multiple messages can appear at the same time. For example:

Using index condition; Using where

Basic analytical ideas

When analyzing the execution plan, you can check it in the following order:

  1. Verify that the order of access to the tables is reasonable.

  2. check type Whether there is a large list ALL or unnecessary index Scan.

  3. contrast possible_keys with key, confirm whether the index is selected.

  4. binding key_len Determine the scope of use of the union index.

  5. attention rows、filtered and Extra Sort, temporary table, and connection buffer information in.

  6. Use of key SQL EXPLAIN ANALYZE, verify the estimation results with the actual number of rows and time consuming.

Written by AI-rewritten

If you enjoyed this, leave a comment~

Loading...