explain --select_type列详解:

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

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

screenshot

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。
screenshot
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中。
screenshot
注意:一个单位的select查询中还包含其他单位select时,我们将内部的select称为子查询,一个查询中含有子查询时,子查询通常会比外部查询先执行,这种处理方式的速度一般会非常快。但对于select type包含dependent关键字的子查询,由于它要依赖外部查询,所以绝对不会比外部查询先执行。因此,select_type包含dependent关键字的子查询先执行往往会比较低效。
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'
screenshot
上述执行计划的最后一行(union result)的table列显示为表示它是id分别为1和2的单位查询的查询结果的联合。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值