MySQL 查询优化

1147 字
6 分钟
MySQL 查询优化

explain 返回字段说明#

Columnformat=json JSON NAME说明附加说明
idselect_idThe SELECT identifier
select_typeThe SELECT typeSIMPLE
PRIMARY<br>UNION
UNION RESULT<br>SUBQUERY<br>DEPENDENT SUBQUERY<br>DEPENDENT UNION<br>DERIVED<br>MATERIALIZED<br>UNCACHEABLE SUBQUERY<br>UNCACHEABLE UNION
tabletable_nameThe table for the output row
partitionspartitionsThe matching partitions
typeaccess_typeThe join typesystem->const->eq_ref->ref->range->index->all
possible_keyspossible_keysThe possible indexes to choose
keykeyThe index actually chosen
key_lenkey_lengthThe length of the chosen key当 key_len 为 null 时, 说明没有使用到索引.
通过 key_len 计算规则, 可以判断索引字段的使用情况.
refrefThe columns compared to the index
rowsrowsEstimate of rows to be examined
filteredfilteredPercentage of rows filtered by table condition
extraAdditional information

id#

The SELECT identifier. This is the sequential number of the SELECT within the query. The value can be NULL if the row refers to the union result of other rows. In this case, the table column shows a value like <union``***M***``,``***N***``> to indicate that the row refers to the union of the rows with id values of ***M*** and ***N***.

key_len 计算规则#

  1. 可以为 null 的列 key 长度比 非 null 列 key 长度大 1;
  2. 如果索引列是字符型字段, 则索引列数据类型空间占用和字符集有关; | 字符集 | 1个字符集占用字节数 | | --- | --- | | gbk | 2 | | utf8 | 3 | | utf8mb4 | 4 | | latin1 | 1 |
  3. 如果索引列长度是变化的(比如varchar), 则在索引列数据类型本身占用空间的基础上再加 2;

select_type#

he type of SELECT, which can be any of those shown in the following table. A JSON-formatted EXPLAIN exposes the SELECT type as a property of a query_block, unless it is SIMPLE or PRIMARY. The JSON names (where applicable) are also shown in the table.

select_type ValueJSON NameMeaning
**SIMPLE**NoneSimple SELECT (not using UNION or subqueries)
**PRIMARY**NoneOutermost SELECT
**UNION**NoneSecond or later SELECT statement in a UNION
**DEPENDENT UNION**dependent (true)Second or later SELECT statement in a UNION, dependent on outer query
**UNION RESULT**union_resultResult of a UNION.
**SUBQUERY**NoneFirst SELECT in subquery
**DEPENDENT SUBQUERY**dependent (true)First SELECT in subquery, dependent on outer query
**DERIVED**NoneDerived table
**DEPENDENT DERIVED**dependent (true)Derived table dependent on another table
**MATERIALIZED**materialized_from_subqueryMaterialized subquery
**UNCACHEABLE SUBQUERY**cacheable (false)A subquery for which the result cannot be cached and must be re-evaluated for each row of the outer query
**UNCACHEABLE UNION**cacheable (false)The second or later select in a UNION that belongs to an uncacheable subquery (see UNCACHEABLE SUBQUERY)

join types#

system#

The table has only one row (= system table). This is a special case of the const join type.

const#

The table has at most one matching row, which is read at the start of the query. Because there is only one row, values from the column in this row can be regarded as constants by the rest of the optimizer. const tables are very fast because they are read only once.

const is used when you compare all parts of a PRIMARY KEY or UNIQUE index to constant values. In the following queries, ***tbl_name*** can be used as a const table:

SELECT * FROM tbl_name WHERE primary_key=1;
SELECT * FROM tbl_name
WHERE primary_key_part1=1 AND primary_key_part2=2;

eq_ref#

One row is read from this table for each combination of rows from the previous tables. Other than the system and const types, this is the best possible join type. It is used when all parts of an index are used by the join and the index is a PRIMARY KEY or UNIQUE NOT NULL index.

eq_ref can be used for indexed columns that are compared using the = operator. The comparison value can be a constant or an expression that uses columns from tables that are read before this table. In the following examples, MySQL can use an eq_ref join to process ***ref_table***:

SELECT * FROM ref_table,other_table
WHERE ref_table.key_column=other_table.column;
SELECT * FROM ref_table,other_table
WHERE ref_table.key_column_part1=other_table.column
AND ref_table.key_column_part2=1;

ref#

All rows with matching index values are read from this table for each combination of rows from the previous tables. ref is used if the join uses only a leftmost prefix of the key or if the key is not a PRIMARY KEY or UNIQUE index (in other words, if the join cannot select a single row based on the key value). If the key that is used matches only a few rows, this is a good join type.

ref can be used for indexed columns that are compared using the = or <=> operator. In the following examples, MySQL can use a ref join to process ***ref_table***:

SELECT * FROM ref_table WHERE key_column=expr;
SELECT * FROM ref_table,other_table
WHERE ref_table.key_column=other_table.column;
SELECT * FROM ref_table,other_table
WHERE ref_table.key_column_part1=other_table.column
AND ref_table.key_column_part2=1;

fulltext#

The join is performed using a FULLTEXT index.

ref or null#

This join type is like ref, but with the addition that MySQL does an extra search for rows that contain NULL values. This join type optimization is used most often in resolving subqueries. In the following examples, MySQL can use a ref_or_null join to process ***ref_table***:

SELECT * FROM ref_tableWHERE key_column=expr OR key_column IS NULL;

index merge#

This join type indicates that the Index Merge optimization is used. In this case, the key column in the output row contains a list of indexes used, and key_len contains a list of the longest key parts for the indexes used.

unique_subquery#

This type replaces eq_ref for some IN subqueries of the following form:

value IN (SELECT primary_key FROM single_table WHERE some_expr)

index subquery#

This join type is similar to unique_subquery. It replaces IN subqueries, but it works for nonunique indexes in subqueries of the following form:

value IN (SELECT key_column FROM single_table WHERE some_expr)

range#

Only rows that are in a given range are retrieved, using an index to select the rows. The key column in the output row indicates which index is used. The key_len contains the longest key part that was used. The ref column is NULL for this type.

range can be used when a key column is compared to a constant using any of the =<>>>=<<=IS NULL<=>BETWEENLIKE, or IN() operators:

SELECT * FROM tbl_name
WHERE key_column = 10;
SELECT * FROM tbl_name
WHERE key_column BETWEEN 10 and 20;
SELECT * FROM tbl_name
WHERE key_column IN (10,20,30);
SELECT * FROM tbl_name
WHERE key_part1 = 10 AND key_part2 IN (10,20,30);

index#

The index join type is the same as ALL, except that the index tree is scanned. This occurs two ways:

  • If the index is a covering index for the queries and can be used to satisfy all data required from the table, only the index tree is scanned. In this case, the Extra column says Using index. An index-only scan usually is faster than ALL because the size of the index usually is smaller than the table data.
  • A full table scan is performed using reads from the index to look up data rows in index order. Uses index does not appear in the Extra column.

MySQL can use this join type when the query uses only columns that are part of a single index.

ALL#

A full table scan is done for each combination of rows from the previous tables. This is normally not good if the table is the first table not marked const, and usually very bad in all other cases. Normally, you can avoid ALL by adding indexes that enable row retrieval from the table based on constant values or column values from earlier tables.

EXPLAIN Extra Information#

📎 参考文章#

文章分享

如果这篇文章对你有帮助,欢迎分享给更多人!

MySQL 查询优化
https://blog.sephy.top/posts/mysql-查询优化/
作者
虾米
发布于
2024-02-26
许可协议
CC BY-NC-SA 4.0
Profile Image of the Author
虾米
coder
分类
标签
站点统计
文章
61
分类
4
标签
52
总字数
64,663
运行时长
0
最后活动
0 天前
站点信息
构建平台
GitHub Actions
博客版本
Firefly v6.13.9
文章许可
CC BY-NC-SA 4.0