学会使用MySQL的Explain执行计划,SQL性能调优从此不再困难

上篇文章讲了MySQL架构体系,了解到MySQL Server端的优化器可以生成Explain执行计划,而执行计划可以帮助我们分析SQL语句性能瓶颈,优化SQL查询逻辑,今天就一块学习Explain执行计划的具体用法。

1. explain的使用

使用EXPLAIN关键字可以模拟优化器执行SQL语句,分析你的查询语句或是结构的性能瓶颈。
在 select 语句之前增加 explain 关键字,MySQL 会在查询上设置一个标记,执行查询会返回执行计划的信息,并不会执行这条SQL。
就比如下面这个:

学会使用MySQL的Explain执行计划,SQL性能调优从此不再困难

那么多列用于输出的是什么?

[En]

What are so many columns used to output?

其实大都是SQL语句的性能统计指标,先简单总结一下每一列的大致作用,下面详细讲一下:

学会使用MySQL的Explain执行计划,SQL性能调优从此不再困难

2. explain字段详解

让我们详细讨论一下每个专栏的具体作用。

[En]

Let’s talk about the specific role of each column in detail.

1. id列

id表示查询语句的序号,自动分配,顺序递增,值越大,执行优先级越高。

学会使用MySQL的Explain执行计划,SQL性能调优从此不再困难

id相同时,优先级由上而下。

学会使用MySQL的Explain执行计划,SQL性能调优从此不再困难

2. select_type列

select_type表示查询类型,常见的有SIMPLE简单查询、PRIMARY主查询、SUBQUERY子查询、UNION联合查询、UNION RESULT联合临时表结果等。

学会使用MySQL的Explain执行计划,SQL性能调优从此不再困难

3. table列

table表示SQL语句查询的表名、表别名、临时表名。

学会使用MySQL的Explain执行计划,SQL性能调优从此不再困难

4. partitions列

partitions表示SQL查询匹配到的分区,没有分区的话显示NULL。

学会使用MySQL的Explain执行计划,SQL性能调优从此不再困难

5. type列

type表示表连接类型或者数据访问类型,就是表之间通过什么方式建立连接的,或者通过什么方式访问到数据的。

具体取值如下,性能顺序如下:

[En]

The specific values are as follows, and the performance is in the following order:

system > const > eq_ref > ref > fulltext > ref_or_null > index_merge > unique_subquery > index_subquery > range > index > ALL

system

当表中只有一行记录,也就是系统表,是 const 类型的特列。

学会使用MySQL的Explain执行计划,SQL性能调优从此不再困难

const

指示使用主键或唯一索引进行等效查询,并且最多返回一条记录。性能很好,所以推荐使用。

[En]

Indicates that an equivalent query is made using a primary key or a unique index, and at most one record is returned. The performance is good, so it is recommended.

学会使用MySQL的Explain执行计划,SQL性能调优从此不再困难

eq_ref

表示 表连接使用到了主键或者唯一性索引,下面的SQL就用到了user表主键id。

学会使用MySQL的Explain执行计划,SQL性能调优从此不再困难

ref

指示使用非唯一索引进行等效查询。

[En]

Indicates that an equivalent query is made using a non-unique index.

学会使用MySQL的Explain执行计划,SQL性能调优从此不再困难

ref_or_null

表示使用非唯一性索引进行等值查询,并且包含了null值的行。

学会使用MySQL的Explain执行计划,SQL性能调优从此不再困难

index_merge

表示用于索引合并的优化逻辑,即使用多个索引。

[En]

Represents the optimization logic used for index merging, that is, multiple indexes used.

学会使用MySQL的Explain执行计划,SQL性能调优从此不再困难

range

指示使用索引范围查询。

[En]

Indicates that an index range query is used.

学会使用MySQL的Explain执行计划,SQL性能调优从此不再困难

index

指示索引用于全表扫描。

[En]

Indicates that an index is used for a full table scan.

学会使用MySQL的Explain执行计划,SQL性能调优从此不再困难

ALL

表示全表扫描的性能最差。

[En]

Indicates that the full table scan has the worst performance.

学会使用MySQL的Explain执行计划,SQL性能调优从此不再困难

6. possible_keys列

表示可能使用但实际查询可能不可用的索引列。

[En]

Represents the index columns that may be used, but the actual query may not be available.

学会使用MySQL的Explain执行计划,SQL性能调优从此不再困难

7. key列

指示实际查询使用索引列。

[En]

Indicates that the actual query uses an index column.

学会使用MySQL的Explain执行计划,SQL性能调优从此不再困难

8. key_len列

表示索引所占的字节数。

学会使用MySQL的Explain执行计划,SQL性能调优从此不再困难

每种类型占用的字节数如下:

[En]

The number of bytes occupied by each type is as follows:

类型 占用空间 char(n) n个字节 varchar(n) 2个字节存储变长字符串,如果是utf-8,则长度 3n + 2 tinyint 1个字节 smallint 2个字节 int 4个字节 bigint 8个字节 date 3个字节 timestamp 4个字节 datetime 8个字节 字段允许为NULL 额外增加1个字节

9. ref列

表示where语句或者表连接中与索引比较的参数,常见的有const(常量)、func(函数)、字段名。

如果没用到索引,则显示为NULL。

学会使用MySQL的Explain执行计划,SQL性能调优从此不再困难

学会使用MySQL的Explain执行计划,SQL性能调优从此不再困难

学会使用MySQL的Explain执行计划,SQL性能调优从此不再困难

10. rows列

表示执行SQL语句所扫描的行数。

学会使用MySQL的Explain执行计划,SQL性能调优从此不再困难

11. filtered列

表示按条件筛选的表行的百分比。

[En]

Represents the percentage of table rows filtered by criteria.

学会使用MySQL的Explain执行计划,SQL性能调优从此不再困难

用来估算与其他表连接时扫描的行数,row x filtered = 252004 x 10% = 25万行

12. Extra列

它表示一些附加的扩展信息,这些信息不适合在其他列中显示,但它非常重要。

[En]

It represents some additional extended information, which is not suitable for display in other columns, but it is very important.

Using where

表示使用了where条件搜索,但没有使用索引。

学会使用MySQL的Explain执行计划,SQL性能调优从此不再困难

Using index

这意味着使用了重叠索引,即在索引上找到所需的数据,不需要两次查表,因此性能更好。

[En]

It means that the overlay index is used, that is, the required data is found on the index, and there is no need to query the table twice, so the performance is better.

学会使用MySQL的Explain执行计划,SQL性能调优从此不再困难

Using filesort

指示使用外部排序,即排序字段不使用索引。

[En]

Indicates that an external sort is used, that is, the sort field does not use an index.

学会使用MySQL的Explain执行计划,SQL性能调优从此不再困难

Using temporary

指示使用临时表,在下面的示例中使用临时表存储查询结果。

[En]

Indicates that temporary tables are used, which are used in the following example to store query results.

学会使用MySQL的Explain执行计划,SQL性能调优从此不再困难

Using join buffer

指示当表关联时不使用索引,连接高速缓存用于存储临时结果。

[En]

Indicates that the index is not used when the table is associated, and the connection cache is used to store temporary results.

下面的示例中 user_id在两张表中都没有建索引。

学会使用MySQL的Explain执行计划,SQL性能调优从此不再困难

Using index condition

表示用到 索引下推的优化特性。

学会使用MySQL的Explain执行计划,SQL性能调优从此不再困难

知识点总结:

本文详细介绍了Explain使用方式,以及每种参数所代表的含义。无论是工作还是面试,使用Explain优化SQL查询,都是必备的技能,一定要牢记。

下篇再一块学习一下SQL查询的其他优化方式,敬请期待。

学会使用MySQL的Explain执行计划,SQL性能调优从此不再困难

文章持续更新,可以微信搜一搜「 一灯架构 」第一时间阅读更多技术干货。

Original: https://www.cnblogs.com/yidengjiagou/p/16527697.html
Author: 一灯架构
Title: 学会使用MySQL的Explain执行计划,SQL性能调优从此不再困难

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/505136/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球