mysql explain 输出详解

本文详细解析了MySQL的EXPLAIN命令输出的各个字段,包括id、select_type、table、type、possible_keys、key、key_len、ref、rows、Extra等,帮助理解查询执行计划和优化查询性能。
摘要由CSDN通过智能技术生成

explain的输出

mysql> explain select dept_no,count(*) from dept_emp where dept_no='d001';
+----+-------------+----------+------+---------------+---------+---------+-------+-------+--------------------------+
| id | select_type | table    | type | possible_keys | key     | key_len | ref   | rows  | Extra                    |
+----+-------------+----------+------+---------------+---------+---------+-------+-------+--------------------------+
|  1 | SIMPLE      | dept_emp | ref  | dept_no       | dept_no | 4       | const | 41592 | Using where; Using index |
+----+-------------+----------+------+---------------+---------+---------+-------+-------+--------------------------+
1 row in set (0.04 sec)

mysql> explain extended select dept_no,count(*) from dept_emp where dept_no='d001';
+----+-------------+----------+------+---------------+---------+---------+-------+-------+----------+--------------------------+
| id | select_type | table    | type | possible_keys | key     | key_len | ref   | rows  | filtered | Extra                    |
+----+-------------+----------+------+---------------+---------+---------+-------+-------+----------+--------------------------+
|  1 | SIMPLE      | dept_emp | ref  | dept_no       | dept_no | 4       | const | 41592 |   100.00 | Using where; Using index |
+----+-------------+----------+------+---------------+---------+---------+-------+-------+----------+--------------------------+
1 row in set, 1 warning (0.57 sec)

由此可以看出:
当使用explain时,输出中包含的列为:
id,select_type,table,type,possible_keys,key,key_len,ref,rows,Extra

当使用explain extended时,输出中包含的列为:
比使用explain增加了filtered列


各个属性的含义:

id

select查询的序列号,当引用其他查询结果做union时,该值为null,且table列的值为union(m,n),意思是把id为m和n的查询结果做union。

select_type

select查询的类型,主要是区别普通查询和联合查询、子查询之类的复杂查询。

  • simple:简单查询,查询中不包含子查询或者union。
  • primary:查询中若包含复杂的子部分,则最外层查询为primary。
  • union:若第二个select出现在union之后,则被标记为union,如果有多个union,则除第一个select之外,后续的都是union。
  • dependent union:在union中的第二个及以后的查询语句,依赖于外层查询。(关联查询?)
  • union result:union的结果,id列为null,table列显示了是由哪几个查询的结果做的union。
  • subquery:子查询中的第一个查询。(第二个及以后的呢?)
  • dependent query:子查询中的第一个查询,依赖于外部查询。(关联子查询?)
  • derived:衍生查询(from子句中的子查询)???
  • materialized:物化子查询?什么意思?
  • uncacheable subquery:查询结果不能缓存的子查询,对于外部查询中的每一行都必须重新计算。
  • uncacheable union:该查询是union查询中的第二个及以后的查询语句,且整个union查询语句是一个uncacheable subquery。

    关于dependent subquery和uncacheable subquery,官方文档是这样解释的:

DEPENDENT SUBQUERY evaluation differs from UNCACHEABLE SUBQUERY evaluation. For DEPENDENT SUBQUERY, the subquery is re-evaluated only once for each set of different values of the variables from its outer context. For UNCACHEABLE SUBQUERY, the subquery is re-evaluated for each row of the outer context.

翻译如下:
dependent subquery评估和uncacheable subquery评估不同。dependent subquery对于外部查询中的不同的值只计算一次。而uncacheable subquery对于外部查询中的每一行都重新评估一次。
(由此可见两者的执行效率完全不同,极端情况下能差好几个数量级)

table

查询结果出自哪张表。可以是具体的表名,也可以是以下的值:

  • union M,N:id为M和N的查询结果做union
  • derivedN:参考id为N的查询的衍生查询
  • subqueryN:参考id为N的物化子查询

type

表连接类型,访问方式。

type显示的是访问类型,是较为重要的一个指标,结果值从好到坏依次是:
system > const > eq_ref > ref > fulltext > ref_or_null > index_merge > unique_subquery > index_subquery > range > index > ALL
一般来说,得保证查询至少达到range级别,最好能达到ref。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值