执行计划
为了知道SQL语句的执行,需要查看SQL语句的具体执行过程,以优化SQL语句的执行效率。
采用explain SQL语句
来模拟优化器执行SQL语句,从而知道MySQL如何处理sql语句的
1.执行计划包含的信息
id: select查询的序列号,包含一组数字,表示查询中执行select字句或者操作表的顺序
分为3中情况。
- 如果id相同:那么执行顺序从上往下
- 如果id不同:如果是子查询,id的序号会递增,id的值越大优先级越高,越先被执行
- id相同和不同的,同时存在:相同的可以认为是一组,从上往下顺序执行,在所有组中,id值越大,优先级越高,越先执行=
select_type
主要用来分辨查询的类型,是普通查询还是联合查询还是子查询
--sample:简单的查询,不包含子查询和union
explain select * from emp;
--primary:查询中若包含任何复杂的子查询,最外层查询则被标记为Primary
explain select * from user where role_id=(select id from role where name='开发');
--subquery:在select或者where列表中包含子查询。子查询标记为subquery
--SUBQUERY(子查询中的第一个SELECT)
explain select * from user where role_id=(select id from role where name='开发');
--dependent subquery。subquery的子查询要受到外部表查询的影响
explain select * from user where role_id = ( select id from role where id=user.id );
--union:若第二个select出现在union之后,则被标记为union。(select * from emp 是union)
-- 若UNION包含在FROM子句的子查询中,外层SELECT将被标记为:DERIVED
explain select * from emp where deptno = 10 union select * from emp where sal >2000;
--dependent union:跟union类似,此处的depentent表示union或union all联合而成的结果会受外部表影响
explain select * from user a -- primary
where id in (
select b.id from user b where b.id=a.id -- dependent subquery
union
select c.id from role c where c.id=a.role_id --dependent union
);
--union result:从union结果表 select
explain select * from emp where deptno = 10 union select * from emp where sal >2000;
--DERIVED: from子句中出现的子查询,也叫做派生类,
explain select * from --primary
( select u.id from user u where name='Java' --derived 2
union select r.id from role r where id=1 -- union
) a;
--UNCACHEABLE SUBQUERY:表示使用子查询的结果不能被缓存
explain select * from emp where empno = (select empno from emp where deptno=@@sort_buffer_size);
--uncacheable union:表示union的查询结果不能被缓存:sql语句未验证
table
对应行正在访问哪一个表,表名或者别名,可能是临时表或者union合并结果集
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
详情:
- eq_ref:使用唯一索引进行数据查找。当使用的是主键索引或者唯一索引来进行连接使用时就会使用eq_ref
--all:全表扫描,一般情况下出现这样的sql语句而且数据量比较大的话那么就需要进行优化。
explain select * from emp;
--index:全索引扫描这个比all的效率要好
-- 主要有两种情况,一种是当前的查询时覆盖索引,即我们需要的数据在索引中就可以索取,或者是使用了索引进行排序,这样就避免数据的重排序
explain select empno from emp;
--range:表示利用索引查询的时候限制了范围,在指定范围内进行查询,这样避免了index的全索引扫描,适用的操作符: =, <>, >, >=, <, <=, IS NULL, BETWEEN, LIKE, or IN()
explain select * from emp where empno between 7000 and 7500;
--index_subquery:利用索引来关联子查询,不再扫描全表
explain select * from emp where emp.job in (select job from t_job);
--unique_subquery:该连接类型类似与index_subquery,使用的是唯一索引
explain select * from emp e where e.deptno in (select distinct deptno from dept);
--index_merge:在查询过程中需要多个索引组合使用
--ref_or_null:对于某个字段即需要关联条件,也需要null值的情况下,查询优化器会选择这种访问方式
explain select * from emp e where e.mgr is null or e.mgr=7369;
--ref:使用了非唯一性索引进行数据的查找
--eq_ref :使用唯一性索引进行数据查找
explain select * from emp,emp2 where emp.empno = emp2.empno;
--const:这个表至多有一个匹配行
explain select * from emp where empno = 7369;
--system:表只有一行记录(等于系统表),这是const类型的特例,平时不会出现
possible_keys
显示可能用在这张表上的索引,一个或多个,查询涉及到的字段上若存在索引,则该索引被列出,但是不一定被查询实际使用
key
实际使用的索引,如果为null,则没有使用索引
key_len
表示索引中使用的字节数,可以通过key_len计算查询中使用索引的长度,在不损失精确度的情况下,长度越短越好
ref
显示索引的哪一列被使用了,如果可能的话,是一个常数。哪些列或常量被用于查找索引列上的值
rows
根据表的统计信息及索引使用情况,大致估算出找出所需记录需要读取的行数,此参数很重要,直接反应的sql找了多少数据,在完成目的的情况下越少越好。(越少越好)
extra
--using filesort:说明mysql无法利用索引进行排序,只能利用排序算法进行排序,会消耗额外的位置
explain select * from emp order by sal;
--using temporary:建立临时表来保存中间结果,查询完成之后把临时表删除
explain select ename,count(*) from emp where deptno = 10 group by ename;
--using index:这个表示当前的查询时覆盖索引的,直接从索引中读取数据,而不用访问数据表。
--如果同时出现using where 表明索引被用来执行索引键值的查找,如果没有,表面索引被用来读取数据,而不是真的查找
explain select deptno,count(*) from emp group by deptno limit 10;
--using where:使用where进行条件过滤
explain select * from t_user where id = 1;
--using join buffer:使用连接缓存,
--impossible where:where语句的结果总是false
explain select * from emp where empno = 7469;
参考:cnblogs.com/sunjingwu/p/10755823.html