Mysql执行计划

执行计划中包含的信息
官网地址:https://dev.mysql.com/doc/refman/5.5/en/explain-output.html

id

select查询的序列号。
id越大越优先执行,id一样就按照顺序执行。

select_type

主要用来分辨查询类型,普通还是联合

  1. SIMPE:简单查询,不包括子查询和union
    explain select * from emp;
    在这里插入图片描述

  2. PRIMARY:查询中若包含任何子查询或者union,最外层查询被标记为primary

  3. UNION:union后面的查询语句被标记为union
    explain select * from emp where deptno = 10 union select * from emp where sal >2000;
    在这里插入图片描述
    A,B求全集,A标记为PRIMARY,B标记为UNION。

  4. DEPENDENT UNION:此处的depentent表示union或union all联合而成的结果会受外部表影响
    explain select * from emp e where e.empno in ( select empno from emp where deptno = 10 union select empno from emp where sal >2000)
    在这里插入图片描述

  5. UNION RESULT:从union表获取结果的select
    explain select * from emp where deptno = 10 union select * from emp where sal >2000;
    在这里插入图片描述

  6. SUBQUERY:在select或者where列表中包含子查询
    explain select * from emp where sal > (select avg(sal) from emp);
    在这里插入图片描述

  7. DEPENDENT SUBQUERY:subquery的子查询要受到外部表查询的影响。
    explain select * from emp e where e.deptno in (select distinct deptno from dept);
    在这里插入图片描述
    加了dependent和没加dependent区别:如果后面查到的是一个的话【等值匹配,判断关系】,就是subquery;如果后面查到的是n多个值(一个结果集合的话)【依赖关系】就是dependent subquery。

  8. DERIVED: from子句中出现的子查询,也叫做派生类
    所有在sql中出现的虚拟表都可以叫做“衍生表”

  9. UNCACHEABLE SUBQUERY:表示使用子查询的结果不能被缓存
    explain select * from emp where empno = (select empno from emp where deptno=@@sort_buffer_size);
    在这里插入图片描述

  10. uncacheable union:表示union的查询结果不能被缓存:sql语句未验证

table

  1. 如果是具体的表名(或别名),则表明从实际的物理表中获取数据
  2. 表名是derivedN的形式,表示使用了id为N的查询产生的衍生表
  3. 当有union result的时候,表名是union n1,n2等的形式,n1,n2表示参与union的id

type

显示的是访问类型。以何种方法去访问我们的数据。
效率从最好到最坏依次是:
system > const > eq_ref > ref > fulltext > ref_or_null > index_merge > unique_subquery > index_subquery > range > index > ALL

一般情况下,得保证查询至少达到range级别,最好能达到ref

  1. all:全表扫描,一般情况下出现这样的sql语句而且数据量比较大的话那么就需要进行优化
  2. index:表示查询的是某一个主键列
    explain select empno from emp;
    在这里插入图片描述
  3. range:表示利用索引查询的时候限制了范围,在指定范围内进行查询,这样避免了index的全索引扫描,适用的操作符: =, <>, >, >=, <, <=, IS NULL, BETWEEN, LIKE, or IN()
    explain select * from emp where empno between 7000 and 7500;
    一定要使用索引。如果是all一定没用索引。下图因为sal没用建索引,所以只能一行一行遍历。
    在这里插入图片描述
  4. index_subquery:利用索引来关联子查询,不再扫描全表。也必须是索引列才可以使用。
    explain select * from emp where emp.job in (select job from t_job);
    在这里插入图片描述
  5. unique_subquery:该连接类型类似与index_subquery,使用的是唯一索引
    explain select * from emp e where e.deptno in (select distinct deptno from dept);
    在这里插入图片描述
    刚刚index_subquery是给普通列建索引,而这个是给唯一列(主键列)建的索引。
  6. index_merge:在查询过程中需要多个索引组合使用
  7. ref_or_null:对于某个字段即需要关联条件,也需要null值的情况下,查询优化器会选择这种访问方式
    在这里插入图片描述
  8. ref:使用了非唯一性索引进行数据的查找
    先创建非唯一性索引:create index idx_3 on emp(deptno);
  9. eq_ref:使用唯一性索引进行数据查找
    emp2和emp表一样,只是没用主键。
    在这里插入图片描述
  10. const:这个表至多有一个匹配行
    explain select * from emp where empno = 7369;
    在这里插入图片描述
  11. system:表只有一行记录(等于系统表),这是const类型的特例,平时不会出现。

    mysql中有个mysql库,这里面的表一般都是系统表。但里面的值都是多行,不是一行

possible_keys

查询中可能会用到的索引列,可能会用到也可能不会。
explain select * from emp,dept where emp.deptno = dept.deptno and emp.deptno = 10;
在这里插入图片描述
idx_3:自己创建的deptno的索引

key

​实际使用的索引,如果为null,则没有使用索引,查询中若使用了覆盖索引,则该索引和查询的select字段重叠。
在这里插入图片描述

key_len

可以通过key_len计算查询中使用的索引长度,推荐短的(比如name和age的,选择age字节长度短)

ref

显示索引的哪一列被使用了,如果可能的话,是一个常数

rows

根据表的统计信息及索引使用情况,大致估算出找出所需记录需要读取的行数,此参数很重要,直接反应了sql找了多少数据,在完成目的的情况下越少越好。

extra

包含额外的信息
using filesort: 说明mysql无法利用索引进行排序,只能利用排序算法进行排序,会消耗额外的位置
在这里插入图片描述

using temporary:建立临时表来保存中间结果,查询完成之后把临时表删除
using index:这个表示当前的查询时覆盖索引的,直接从索引中读取数据,而不用访问数据表。如果同时出现using where 表名索引被用来执行索引键值的查找,如果没有,表面索引被用来读取数据,而不是真的查找
在这里插入图片描述