mysql的explain解释使用方法

来源:互联网 发布:科比nba数据统计 编辑:程序博客网 时间:2024/05/20 03:47

explain的作用: 判断mysql语句执行的时间效率分析;


1、在navicate客户端使用sql语句的时候,整体图形如下

     

2、名词解释

    一、select_type数据列指明各“单位select 查询”的查询类型,select_type数据列的列值如下所示:

    1.simple:
    进行不需要Union操作或不含子查询的简单select查询时,响应查询语句的select_type 即为simple(查询中包含连接的情形也一样)。无论查询语句是多么复杂,执行计划中select_type为simple的单位查询一定只有一个。最外侧的select查询的select_type通过为simple

     EG:SELECT user_idamount FROM coupons WHERE locked= 0 AND expired_at= 1476892800

  2.primary
   一个需要Union操作或含子查询的select查询执行计划中,位于最外层的select_type即为primary。与simple一样,select_type为primary的单位select查询也只存在1个,位于查询最外侧的select单位查询的select_type为primary;

   3.union
由union操作联合而成的单位select查询中,除第一个外,第二个以后的所有单位select查询的select_type都为union。union的第一个单位select的select_type不是union,而是DERIVED。它是一个临时表,用于存储联合(Union)后的查询结果。

explain  select * from(

(select emp_no from employees el limit 10)
union all 
(select emp_no from employees e2 limit 10)
union all
(select emp_no from employees e3 limit 10)
) tb;
以上查询的执行计划如下图所示。3个联合(union)的select 查询中,只有第一个(el数据表)不是union,其余两个的select_type均为union。union的第一个查询设置为代表整个union结果的select_type类型。此外要将3个子查询的结果用union all进行联合,并创建临时表进行使用,所以union all的第一个查询的select_type为DERIVED。

   4.DEPENDENT UNION dependent
    与UNION select_type一样,dependent union出现在union或union all 形成的集合查询中。此处的dependent表示union或union all联合而成的单位查询受外部影响。下列查询中,两个select 查询用union联合起来,所一union出阿信在select_type中,从in所包含的子查询中可以看到,两个查询通过union连接在一起。MariaDB中,不会在默认优化器模式下先处理IN(subquery)查询内部的子查询,而是读取外部的employees数据表,再执行子查询时,dependent关键字就会出现在select_type中。
explain 
select *
from employees e1
where e1.emp_no in(
select e2.emp_no from employees e2 where e2.first_name='Matt'
union
select e3.emp_no from employees e3 where e3.first_name='Matt'
);
最后,内部“e2.emp_no=e1.emp_no” 与“e3.emp_no=e1.emp_no”条件会自动添加到union所使用的select查询的where条件,然后再执行。由于外部定义的employees数据表的emp_no数据列要在子查询中使用,所以dependent union关键字出现在select_type中。

   5、union result

   union result为包含union结果的数据表。MariaDB中,union all或union(DISTINCT)查询会将所有union结果创建为临时表。执行计划中,该临时表所在行为select_type为        union result。由于union result在实际查询中不是单位查询,所以没有单独的id值。

   explain
   select emp_no from salaries where salary>100000
  union all
  select emp_no from dept_emp where from_date>'2001-01-01'


好的学习地址:http://blog.csdn.net/zhuxineli/article/details/14455029