mysql执行计划详解、各字段说明

发布时间:2022-03-01 10:42:26 作者:yexindonglai@163.com 阅读(569)

mysql执行计划

​ 在企业的应用场景中,为了知道优化SQL语句的执行,需要查看SQL语句的具体执行过程,以加快SQL语句的执行效率。

​ 可以使用explain+SQL语句来模拟优化器执行SQL查询语句,从而知道mysql是如何处理sql语句的。

​ 官网地址: https://dev.mysql.com/doc/refman/5.5/en/explain-output.html

mysql那三张著名的练习表

在介绍explain 执行计划之前,我们先把这三张表提前建好,一会要用到;

  1. -- 部门表
  2. create table dept(
  3. deptno int primary key, -- 部门编号
  4. dname varchar(14) , -- 部门名称
  5. loc varchar(13) -- 部门地址
  6. ) ;
  7. insert into dept values (10,'accounting','new york');
  8. insert into dept values (20,'research','dallas');
  9. insert into dept values (30,'sales','chicago');
  10. insert into dept values (40,'operations','boston');
  11. -- 员工表
  12. create table emp
  13. (
  14. empno int primary key, -- 员工编号
  15. ename varchar(10), -- 员工名称
  16. job varchar(9), -- 工作
  17. mgr double, -- 直属领导编号
  18. hiredate date, -- 入职时间
  19. sal double, -- 工资
  20. comm double, -- 奖金
  21. deptno int, -- 部门号
  22. foreign key(deptno) references dept(deptno) -- 添加外键
  23. );
  24. insert into emp values
  25. (7369,'smith','clerk',7902,'1980-12-17',800,null,20);
  26. insert into emp values
  27. (7499,'allen','salesman',7698,'1981-02-20',1600,300,30);
  28. insert into emp values
  29. (7521,'ward','salesman',7698,'1981-02-22',1250,500,30);
  30. insert into emp values
  31. (7566,'jones','manager',7839,'1981-04-02',2975,null,20);
  32. insert into emp values
  33. (7654,'martin','salesman',7698,'1981-09-28',1250,1400,30);
  34. insert into emp values
  35. (7698,'blake','manager',7839,'1981-05-01',2850,null,30);
  36. insert into emp values
  37. (7782,'clark','manager',7839,'1981-06-09',2450,null,10);
  38. insert into emp values
  39. (7788,'scott','analyst',7566,'1987-07-13',3000,null,20);
  40. insert into emp values
  41. (7839,'king','president',null,'1981-11-17',5000,null,10);
  42. insert into emp values
  43. (7844,'turner','salesman',7698,'1981-09-08',1500,0,30);
  44. insert into emp values
  45. (7876,'adams','clerk',7788,'1987-07-13',1100,null,20);
  46. insert into emp values
  47. (7900,'james','clerk',7698,'1981-12-03',950,null,30);
  48. insert into emp values
  49. (7902,'ford','analyst',7566,'1981-12-03',3000,null,20);
  50. insert into emp values
  51. (7934,'miller','clerk',7782,'1982-01-23',1300,null,10);
  52. -- 工资等级表
  53. create table salgrade
  54. (
  55. grade int, -- 工资等级
  56. losal double, -- 最低工资
  57. hisal double -- 最高工资
  58. );
  59. insert into salgrade values (1,700,1200);
  60. insert into salgrade values (2,1201,1400);
  61. insert into salgrade values (3,1401,2000);
  62. insert into salgrade values (4,2001,3000);
  63. insert into salgrade values (5,3001,9999);

执行计划中包含的信息

Column Meaning
id select查询的序列号
select_type 查询类型
table 正在访问的表
partitions 匹配的分区
type 访问类型 ,以何种方式去访问我们的数
possible_keys 可能应用在这张表中的索引,一个或多个
key 实际使用的索引
key_len 索引中使用的字节数
ref 索引的哪一列被使用了
rows 大致估算出找出所需记录需要读取的行数
filtered 按表条件筛选的行百分比
extra 额外的信息

id

select查询的序列号,包含一组数字,表示查询中执行select子句或者操作表的顺序

id号分为三种情况:

​ 1、如果id相同,那么执行顺序从上到下

  1. explain select * from emp e join dept d on e.deptno = d.deptno join salgrade sg on e.sal between sg.losal and sg.hisal;
  2. +----+-------------+-------+------+---------------+--------+---------+---------------+------+----------------------------------------------------+
  3. | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
  4. +----+-------------+-------+------+---------------+--------+---------+---------------+------+----------------------------------------------------+
  5. | 1 | SIMPLE | d | ALL | PRIMARY | NULL | NULL | NULL | 4 | NULL |
  6. | 1 | SIMPLE | e | ref | deptno | deptno | 5 | test.d.deptno | 1 | NULL |
  7. | 1 | SIMPLE | sg | ALL | NULL | NULL | NULL | NULL | 5 | Using where; Using join buffer (Block Nested Loop) |
  8. +----+-------------+-------+------+---------------+--------+---------+---------------+------+----------------------------------------------------+
  9. 3 rows in set (0.00 sec)

​ 2、如果id不同,如果是子查询,id的序号会递增,id值越大优先级越高,越先被执行,下面列子中,id为2的优先执行

  1. explain select * from emp e where e.deptno = (select d.deptno from dept d where d.dname = 'SALES');
  2. +----+-------------+-------+------+---------------+--------+---------+-------+------+-------------+
  3. | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
  4. +----+-------------+-------+------+---------------+--------+---------+-------+------+-------------+
  5. | 1 | PRIMARY | e | ref | deptno | deptno | 5 | const | 6 | Using where |
  6. | 2 | SUBQUERY | d | ALL | NULL | NULL | NULL | NULL | 4 | Using where |
  7. +----+-------------+-------+------+---------------+--------+---------+-------+------+-------------+
  8. 2 rows in set (0.00 sec)

​ 3、id相同和不同的,同时存在:相同的可以认为是一组,从上往下顺序执行,在所有组中,id值越大,优先级越高,越先执行,下列实例的执行顺序为:

  1. 第四行,最先执行
  2. 第一行,第2顺序
  3. 第二行,第3顺序
  4. 第三行,第4顺序
  1. explain select * from emp e join dept d on e.deptno = d.deptno join salgrade sg on e.sal between sg.losal and sg.hisal where e.deptno = (select d.deptno from dept d where d.dname = 'SALES');
  2. +----+-------------+-------+-------+---------------+---------+---------+-------+------+----------------------------------------------------+
  3. | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
  4. +----+-------------+-------+-------+---------------+---------+---------+-------+------+----------------------------------------------------+
  5. | 1 | PRIMARY | d | const | PRIMARY | PRIMARY | 4 | const | 1 | NULL |
  6. | 1 | PRIMARY | sg | ALL | NULL | NULL | NULL | NULL | 5 | NULL |
  7. | 1 | PRIMARY | e | ALL | deptno | NULL | NULL | NULL | 14 | Using where; Using join buffer (Block Nested Loop) |
  8. | 2 | SUBQUERY | d | ALL | NULL | NULL | NULL | NULL | 4 | Using where |
  9. +----+-------------+-------+-------+---------------+---------+---------+-------+------+----------------------------------------------------+
  10. 4 rows in set (0.00 sec)

select_type

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

select_type Value Meaning
SIMPLE 简单的select查询,查询中不包含子查询或者UNION
PRIMARY 查询中包含任意复杂的子部分,最外层查询会被标记为primary
UNION 若第二个SELECT出现在UNION之后,则被标记为UNION若UNION包含在FROM子句的子查询中,外层SELECT将被标记为:DERIVED
DEPENDENT UNION UNION操作中,查询中处于内层的SELECT(内层的SELECT语句与外层的SELECT语句有依赖关系)
UNION RESULT 从UNION表获取结果的SELECT
SUBQUERY 在SELECT或WHERE列表中包含了子查询
DEPENDENT SUBQUERY 子查询中首个SELECT(如果有多个子查询存在)
DERIVED 在FROM列表中包含的子查询被标记为DERIVED(衍生)MYSQL会递归执行这些子查询,把结果放在临时表里
UNCACHEABLE SUBQUERY 对于外层的主表,子查询不可被物化,每次都需要计算(耗时操作)
UNCACHEABLE UNION UNION操作中,内层的不可被物化的子查询(类似于UNCACHEABLE SUBQUERY)
  1. --sample:简单的查询,不包含子查询和union
  2. explain select * from emp;
  3. --primary:查询中若包含任何复杂的子查询,最外层查询则被标记为Primary
  4. explain select staname,ename supname from (select ename staname,mgr from emp) t join emp on t.mgr=emp.empno ;
  5. --union:若第二个select出现在union之后,则被标记为union
  6. explain select * from emp where deptno = 10 union select * from emp where sal >2000;
  7. --dependent union:跟union类似,此处的depentent表示unionunion all联合而成的结果会受外部表影响
  8. explain select * from emp e where e.empno in ( select empno from emp where deptno = 10 union select empno from emp where sal >2000);
  9. --union result:从union表获取结果的select
  10. explain select * from emp where deptno = 10 union select * from emp where sal >2000;
  11. --subquery:在select或者where列表中包含子查询
  12. explain select * from emp where sal > (select avg(sal) from emp) ;
  13. --dependent subquery:subquery的子查询要受到外部表查询的影响
  14. explain select * from emp e where e.deptno in (select distinct deptno from dept);
  15. --DERIVED: from子句中出现的子查询,也叫做派生类,
  16. explain select staname,ename supname from (select ename staname,mgr from emp) t join emp on t.mgr=emp.empno ;
  17. --UNCACHEABLE SUBQUERY:表示使用子查询的结果不能被缓存
  18. explain select * from emp where empno = (select empno from emp where deptno=@@sort_buffer_size);
  19. --uncacheable union:表示union的查询结果不能被缓存:sql语句未验证

table

对应行正在访问哪一个表,表名或者别名,可能是临时表或者union合并结果集
1、如果是具体的表名,则表明从实际的物理表中获取数据,当然也可以是表的别名

​ 2、表名是derivedN的形式,表示使用了id为N的查询产生的衍生表

​ 3、当有union result的时候,表名是union n1,n2等的形式,n1,n2表示参与union的id

type(很重要)

type显示的是访问类型,访问类型表示我是以何种方式去访问我们的数据,最容易想的是全表扫描,直接暴力的遍历一张表去寻找需要的数据,效率非常低下,访问的类型有很多,效率从最好到最坏依次是:

  1. system -- 最快
  2. const
  3. eq_ref
  4. ref
  5. fulltext
  6. ref_or_null
  7. index_merge
  8. unique_subquery
  9. index_subquery
  10. range
  11. index
  12. ALL --最慢

这个字段非常重要,是衡量优化sql的唯一标准,一般情况下,得保证查询至少达到range级别,最好能达到ref

  1. --all:全表扫描,一般情况下出现这样的sql语句而且数据量比较大的话那么就需要进行优化。
  2. explain select * from emp;
  3. --index:全索引扫描这个比all的效率要好,主要有两种情况,一种是当前的查询时覆盖索引,即我们需要的数据在索引中就可以索取,或者是使用了索引进行排序,这样就避免数据的重排序
  4. explain select empno from emp;
  5. --range:表示利用索引查询的时候限制了范围,在指定范围内进行查询,这样避免了index的全索引扫描,适用的操作符: =, <>, >, >=, <, <=, IS NULL, BETWEEN, LIKE, or IN()
  6. explain select * from emp where empno between 7000 and 7500;
  7. --index_subquery:利用索引来关联子查询,不再扫描全表
  8. explain select * from emp where emp.job in (select job from t_job);
  9. --unique_subquery:该连接类型类似与index_subquery,使用的是唯一索引
  10. explain select * from emp e where e.deptno in (select distinct deptno from dept);
  11. --index_merge:在查询过程中需要多个索引组合使用,没有模拟出来
  12. --ref_or_null:对于某个字段即需要关联条件,也需要null值的情况下,查询优化器会选择这种访问方式
  13. explain select * from emp e where e.mgr is null or e.mgr=7369;
  14. --ref:使用了非唯一性索引进行数据的查找
  15. create index idx_3 on emp(deptno);
  16. explain select * from emp e,dept d where e.deptno =d.deptno;
  17. --eq_ref :使用唯一性索引进行数据查找
  18. explain select * from emp,emp2 where emp.empno = emp2.empno;
  19. --const:这个表至多有一个匹配行,
  20. explain select * from emp where empno = 7369;
  21. --system:表只有一行记录(等于系统表),这是const类型的特例,平时不会出现

possible_keys

​ 显示可能应用在这张表中的索引,一个或多个,查询涉及到的字段上若存在索引,则该索引将被列出,但不一定被查询实际使用

  1. explain select * from emp,dept where emp.deptno = dept.deptno and emp.deptno = 10;

key

​ 实际使用的索引,如果为null,则没有使用索引,查询中若使用了覆盖索引,则该索引和查询的select字段重叠。

  1. explain select * from emp,dept where emp.deptno = dept.deptno and emp.deptno = 10;

key_len

表示索引中使用的字节数,可以通过key_len计算查询中使用的索引长度,在不损失精度的情况下长度越短越好。

  1. explain select * from emp,dept where emp.deptno = dept.deptno and emp.deptno = 10;

ref

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

  1. explain select * from emp,dept where emp.deptno = dept.deptno and emp.deptno = 10;

rows

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

  1. explain select * from emp;

extra
包含额外的信息。以下举例几个常见的信息说明

extra信息 说明
using filesort 无法利用索引进行排序,使用文件排序
using temporary 建立临时表来保存中间结果
using index 查询时使用了覆盖索引
using where 使用where进行条件过滤
using join buffer 使用连接缓存
Impossible WHERE noticed after reading const tables 未查到数据
  1. --using filesort:说明mysql无法利用索引进行排序,只能利用排序算法进行排序,会消耗额外的位置
  2. explain select * from emp order by sal;
  3. --using temporary:建立临时表来保存中间结果,查询完成之后把临时表删除
  4. explain select ename,count(*) from emp where deptno = 10 group by ename;
  5. --using index:这个表示当前的查询时覆盖索引的,直接从索引中读取数据,而不用访问数据表。如果同时出现using where 表名索引被用来执行索引键值的查找,如果没有,表面索引被用来读取数据,而不是真的查找
  6. explain select deptno,count(*) from emp group by deptno limit 10;
  7. --using where:使用where进行条件过滤,至于有没有用到索引具体得看 type 字段
  8. explain select * from t_user where id = 1;
  9. --using join buffer:使用连接缓存,情况没有模拟出来
  10. -- Impossible WHERE noticed after reading const tables :阅读常量表后发现不可能的地方,其实就是没查到数据,结果是空的
  11. explain select * from emp where empno = 7469;

关键字Mysql