Mysql Explain详解

一、背景

在日常工作中,可能会收到一些超时或慢响应的告警,最根到底可能是因为一些执行时间比较的SQL语句,这就跟我们平时开发需要注意细节相关了。那么找到这些SQL语句怎么优化呢?到底是哪里的问题导致SQL执行时间长呢? 这个时候Explain命令尤其重要,它可以查看该SQL语句有没有使用上索引、使用了哪个索引、有没有做全表扫描、有没有使用临时表等等。下面都是基于mysql 8进行案例说明的。

二、语法

EXPLAIN语句提供有关MySQL如何执行语句的信息。 EXPLAIN 通常与SELECT,DELETE,INSERT,REPLACE和UPDATE语句一起使用。

例如:explain select * from tb_student;

三、explain 输出列详解

Column

JSON Name

Meaning

id

select_id

The SELECT identifier

select_type

None

The SELECT type

table

table_name

The table for the output row

partitions

partitions

The matching partitions

type

access_type

The join type

possible_keys

possible_keys

The possible indexes to choose

key

key

The index actually chosen

key_len

key_length

The length of the chosen key

ref

ref

The columns compared to the index

rows

rows

Estimate of rows to be examined

filtered

filtered

Percentage of rows filtered by table condition

Extra

None

Additional information

example:

1

2

3

4

5

+----+-------------+-------+------------+-------+---------------+--------+---------+------+------+----------+-------------+

| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |

+----+-------------+-------+------------+-------+---------------+--------+---------+------+------+----------+-------------+

| 1 | SIMPLE | t | NULL index NULL | idx_id | 5 | NULL | 1 | 100.00 | Using index |

+----+-------------+-------+------------+-------+---------------+--------+---------+------+------+----------+-------------+

  1. id:select标志符,有几个 select 就有几个id,并且id的顺序是按 select 出现的顺序增长的。MySQL将 select 查询分为简单查询和复杂查询。复杂查询可以如下:

    1

    2

    3

    4

    5

    6

    7

    mysql> explain select (select from t limit 1) from t1;

    +----+-------------+-------+------------+-------+---------------+---------+---------+------+------+----------+-------------+

    | id | select_type | table | partitions | type  | possible_keys | key     | key_len | ref  | rows | filtered | Extra       |

    +----+-------------+-------+------------+-------+---------------+---------+---------+------+------+----------+-------------+

    |  1 | PRIMARY     | t1    | NULL       index NULL          PRIMARY | 4       | NULL |    1 |   100.00 | Using index |

    |  2 | SUBQUERY    | t     | NULL       index NULL          | idx_id  | 5       | NULL |    1 |   100.00 | Using index |

    +----+-------------+-------+------------+-------+---------------+---------+---------+------+------+----------+-------------+

  1. select_type

select_type 表示对应行是是简单还是复杂的查询。总共有12种类型,挑其中几种描述下:

    • simple:简单查询。查询不包含子查询和union

      1

      2

      3

      4

      5

      6

      mysql> explain select from t;

      +----+-------------+-------+------------+-------+---------------+--------+---------+------+------+----------+-------------+

      | id | select_type | table | partitions | type  | possible_keys | key    

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值