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';
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

Traditional table formats usually contain the following columns:
| listing | role |
|---|---|
id | Query block number, which can help determine the execution relationship between query blocks |
select_type | The type of query block, such as simple query, main query, subquery, or derived table |
table | Table, alias, or internal temporary result currently accessed |
partitions | Partition expected to be accessed; usually when partition tables are not used NULL |
type | Table access method is an important indicator to determine index usage |
possible_keys | Index that the optimizer thinks is likely to use |
key | Actual selected index |
key_len | Expected index key length in bytes |
ref | Constant or column to compare to indexed column |
rows | The optimizer estimates the number of rows to check |
filtered | Percentage expected to retain after filtering through current table conditions |
Extra | Supplementary 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.
idThe 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.idDifferent usually indicates the presence of a subquery, derived table, orUNIONWait for multiple query blocks.- Certain results are consistent
idmay beNULL, for exampleUNION 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:
| value | meaning |
|---|---|
SIMPLE | not contain UNION or subquery |
PRIMARY | outermost query |
UNION | UNION The second and subsequent query blocks in |
DEPENDENT UNION | Dependent on outer query results UNION query block |
UNION RESULT | UNION result set |
SUBQUERY | Subqueries that do not rely on outer queries |
DEPENDENT SUBQUERY | Related subqueries that rely on outer query results |
DERIVED | FROM Derived table in clause |
MATERIALIZED | Subquery 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 numberedNThe derived table query block of.<unionM,N>: From the number numberedM、NWait for query blockUNIONresult.<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.
| type | description |
|---|---|
system | Table is treated as having only one row, yes const a special case of |
const | Returns up to one row through equivalent matching of the primary key or unique index |
eq_ref | When joining, match is made through the primary key or non-empty unique index, and each previous table combination hits at most one row |
ref | Equivalent lookup through incomplete prefixes of a common index or unique index may return multiple rows |
fulltext | Use full-text indexing |
ref_or_null | similar ref, while looking for additional NULL |
index_merge | Merge scan results from multiple indexes |
unique_subquery | certain IN Subquery looks up through a unique index |
index_subquery | certain IN Subquery looks up through non-unique index |
range | Performs a range scan on the index |
index | Scan the entire index |
ALL | Scan 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 ÷ 100If you need to observe the actual execution rows and time spent, you should use the EXPLAIN ANALYZE。
Extra: Supplementary information
Extra Common information in:
| information | description |
|---|---|
Using index | Use the override index to get the columns you need, usually without going back to the table |
Using where | After reading the data, it also needs to be filtered based on conditions |
Using index condition | Use index conditions to push down and filter some records at the storage engine level |
Using filesort | Sorting cannot directly utilize the appropriate index and requires additional sorting; you don’t have to actually write disk files |
Using temporary | Use internal temporary tables to hold intermediate results, commonly used in partial grouping, deduplication, or sorting operations |
Using join buffer | The connection process uses connection buffers and usually requires checking connection conditions and indexes |
FirstMatch | One of the semi-connection optimization strategies, stop searching after finding the first match |
LooseScan | One of the semi-connection optimization strategies is to reduce duplicate matches through index skip scanning |
Impossible WHERE | The optimizer determines that the query condition cannot be established |
Extra Multiple messages can appear at the same time. For example:
Using index condition; Using whereBasic analytical ideas
When analyzing the execution plan, you can check it in the following order:
-
Verify that the order of access to the tables is reasonable.
-
check
typeWhether there is a large listALLor unnecessaryindexScan. -
contrast
possible_keyswithkey, confirm whether the index is selected. -
binding
key_lenDetermine the scope of use of the union index. -
attention
rows、filteredandExtraSort, temporary table, and connection buffer information in. -
Use of key SQL
EXPLAIN ANALYZE, verify the estimation results with the actual number of rows and time consuming.
If you enjoyed this, leave a comment~