MySQL执行计划Explain参数详解
优化SQL语句的执行,需要查看SQL语句的具体执行过程,以加快SQL语句的执行效率。可以使用explain+SQL语句来模拟优化器执行SQL查询语句,从而知道mysql是如何处理sql语句的。
官网地址: https://dev.mysql.com/doc/refman/5.5/en/explain-output.html
1 数据初始化
使用Oralce 测试表转换成MySQL。具体表信息如下(存在组件需按顺序执行)
dept 部门表
字段 | 含义 |
---|---|
deptno | 部门编号 |
dname | 部门名称 |
loc | 地点 |
DROP TABLE IF EXISTS `dept`;
CREATE TABLE `dept` (
`DEPTNO` int(2) NOT NULL,
`DNAME` varchar(14) DEFAULT NULL,
`LOC` varchar(13) DEFAULT NULL,
PRIMARY KEY (`DEPTNO`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of dept
-- ----------------------------
INSERT INTO `dept` VALUES ('10', 'ACCOUNTING', 'NEW YORK');
INSERT INTO `dept` VALUES ('20', 'RESEARCH', 'DALLAS');
INSERT INTO `dept` VALUES ('30', 'SALES', 'CHICAGO');
INSERT INTO `dept` VALUES ('40', 'OPERATIONS', 'BOSTON');
emp 员工表
字段 | 含义 |
---|---|
empno | 员工号 |
ename | 员工姓名 |
job | 工作 |
mgr | 上级编号 |
hiredate | 受雇日期 |
sal | 薪金 |
comm | 佣金 |
deptno | 部门编号 |
DROP TABLE IF EXISTS `emp`;
CREATE TABLE `emp` (
`EMPNO` int(4) NOT NULL,
`ENAME` varchar(10) DEFAULT NULL,
`JOB` varchar(9) DEFAULT NULL,
`MGR` int(4) DEFAULT NULL,
`HIREDATE` date DEFAULT NULL,
`SAL` int(7) DEFAULT NULL,
`COMM` int(7) DEFAULT NULL,
`DEPTNO` int(2) DEFAULT NULL,
PRIMARY KEY (`EMPNO`),
KEY `FK_DEPTNO` (`DEPTNO`),
CONSTRAINT `FK_DEPTNO` FOREIGN KEY (`DEPTNO`) REFERENCES `dept` (`DEPTNO`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of emp
-- ----------------------------
INSERT INTO `emp` VALUES ('7369', 'SMITH', 'CLERK', '7902', '1980-12-17', '800', null, '20');
INSERT INTO `emp` VALUES ('7499', 'ALLEN', 'SALESMAN', '7698', '1981-02-20', '1600', '300', '30');
INSERT INTO `emp` VALUES ('7521', 'WARD', 'SALESMAN', '7698', '1981-02-22', '1250', '500', '30');
INSERT INTO `emp` VALUES ('7566', 'JONES', 'MANAGER', '7839', '1981-04-02', '2975', null, '20');
INSERT INTO `emp` VALUES ('7654', 'MARTIN', 'SALESMAN', '7698', '1981-09-28', '1250', '1400', '30');
INSERT INTO `emp` VALUES ('7698', 'BLAKE', 'MANAGER', '7839', '1981-05-01', '2850', null, '30');
INSERT INTO `emp` VALUES ('7782', 'CLARK', 'MANAGER', '7839', '1981-06-09', '2450', null, '10');
INSERT INTO `emp` VALUES ('7788', 'SCOTT', 'ANALYST', '7566', '1987-04-19', '3000', null, '20');
INSERT INTO `emp` VALUES ('7839', 'KING', 'PRESIDENT', null, '1981-11-17', '5000', null, '10');
INSERT INTO `emp` VALUES ('7844', 'TURNER', 'SALESMAN', '7698', '1981-09-08', '1500', '0', '30');
INSERT INTO `emp` VALUES ('7876', 'ADAMS', 'CLERK', '7788', '1987-05-23', '1100', null, '20');
INSERT INTO `emp` VALUES ('7900', 'JAMES', 'CLERK', '7698', '1981-12-03', '950', null, '30');
INSERT INTO `emp` VALUES ('7902', 'FORD', 'ANALYST', '7566', '1981-12-03', '3000', null, '20');
INSERT INTO `emp` VALUES ('7934', 'MILLER', 'CLERK', '7782', '1982-01-23', '1300', null, '10');
salgrade表
DROP TABLE IF EXISTS `salgrade`;
CREATE TABLE `salgrade` (
`grade` int(11) NOT NULL DEFAULT '0',
`losal` int(11) DEFAULT NULL,
`hisal` int(11) DEFAULT NULL,
PRIMARY KEY (`grade`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of salgrade
-- ----------------------------
INSERT INTO `salgrade` VALUES ('1', '700', '1200');
INSERT INTO `salgrade` VALUES ('2', '1201', '1400');
INSERT INTO `salgrade` VALUES ('3', '1401', '2000');
INSERT INTO `salgrade` VALUES ('4', '2001', '3000');
INSERT INTO `salgrade` VALUES ('5', '3001', '9999');
2 执行计划中包含的信息
Column | Meaning |
---|---|
id | The SELECT identifier |
select_type | The SELECT type |
table | The table for the output row |
partitions | The matching partitions |
type | The join type |
possible_keys | The possible indexes to choose |
key | The index actually chosen |
key_len | The length of the chosen key |
ref | The columns compared to the index |
rows | Estimate of rows to be examined |
filtered | Percentage of rows filtered by table condition |
extra | Additional information |
id
select查询的序列号,包含一组数字,表示查询中执行select子句或者操作表的顺序
id号分为三种情况:
- 如果id相同,那么执行顺序从上到下
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;
- 如果id不同,如果是子查询,id的序号会递增,id值越大优先级越高,越先被执行
EXPLAIN SELECT
*
FROM
emp e
WHERE
e.deptno IN ( SELECT d.deptno FROM dept d WHERE d.dname = 'SALES' );
- id相同和不同的,同时存在:相同的可以认为是一组,从上往下顺序执行,在所有组中,id值越大,优先级越高,越先执行
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 IN ( SELECT d.deptno FROM dept d WHERE d.dname = 'SALES' );
select_type
主要用来分辨查询的类型,是普通查询还是联合查询还是子查询
select_type Value |
Meaning |
---|---|
SIMPLE | Simple SELECT (not using UNION or subqueries) |
PRIMARY | Outermost SELECT |
UNION | Second or later SELECT statement in a UNION |
DEPENDENT UNION | Second or later SELECT statement in a UNION, dependent on outer query |
UNION RESULT | Result of a UNION. |
SUBQUERY | First SELECT in subquery |
DEPENDENT SUBQUERY | First SELECT in subquery, dependent on outer query |
DERIVED | Derived table |
UNCACHEABLE SUBQUERY | A subquery for which the result cannot be cached and must be re-evaluated for each row of the outer query |
UNCACHEABLE UNION | The second or later select in a UNION that belongs to an uncacheable subquery (see UNCACHEABLE SUBQUERY) |
- sample:简单的查询,不包含子查询和union
EXPLAIN SELECT
*
FROM
emp;
- primary:查询中若包含任何复杂的子查询,最外层查询则被标记为Primary
EXPLAIN SELECT
staname,
ename supname
FROM
( SELECT ename staname, mgr FROM emp ) t
JOIN emp ON t.mgr = emp.empno;
- union:若第二个select出现在union之后,则被标记为union
EXPLAIN SELECT
*
FROM
emp
WHERE
deptno = 10 UNION
SELECT
*
FROM
emp
WHERE
sal > 2000;
- dependent union:跟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)
- union result:从union表获取结果的select
EXPLAIN SELECT
*
FROM
emp
WHERE
deptno = 10 UNION
SELECT
*
FROM
emp
WHERE
sal > 2000;
- subquery:在select或者where列表中包含子查询
EXPLAIN SELECT
*
FROM
emp
WHERE
sal > ( SELECT avg( sal ) FROM emp );
- dependent subquery:subquery的子查询要受到外部表查询的影响
EXPLAIN SELECT
*
FROM
emp e
WHERE
e.deptno IN ( SELECT DISTINCT deptno FROM dept );
- DERIVED: from子句中出现的子查询,也叫做派生类,
EXPLAIN SELECT
staname,
ename supname
FROM
( SELECT ename staname, mgr FROM emp ) t
JOIN emp ON t.mgr = emp.empno;
- UNCACHEABLE SUBQUERY:表示使用子查询的结果不能被缓存
EXPLAIN SELECT
*
FROM
emp
WHERE
empno = ( SELECT empno FROM emp WHERE deptno = @@sort_buffer_size );
- uncacheable union:表示union的查询结果不能被缓存:sql语句未验证
table
对应行正在访问哪一个表,表名或者别名,可能是临时表或者union合并结果集。
- 如果是具体的表名,则表明从实际的物理表中获取数据,当然也可以是表的别名;
- 表名是derivedN的形式,表示使用了id为N的查询产生的衍生表;
- 当有union result的时候,表名是union n1,n2等的形式,n1,n2表示参与union的id;
type
type显示的是访问类型,访问类型表示我是以何种方式去访问我们的数据,最容易想的是全表扫描,直接暴力的遍历一张表去寻找需要的数据,效率非常低下,访问的类型有很多,效率从最好到最坏依次是:
system > const > eq_ref > ref > fulltext > ref_or_null > index_merge > unique_subquery > index_subquery > range > index > ALL
一般情况下,得保证查询至少达到range级别,最好能达到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:使用了非唯一性索引进行数据的查找
CREATE INDEX idx_3 ON emp ( deptno );
EXPLAIN SELECT
*
FROM
emp e,
dept d
WHERE
e.deptno = d.deptno;
- eq_ref :使用唯一性索引进行数据查找
EXPLAIN SELECT
*
FROM
emp,
emp2
WHERE
emp.empno = emp2.empno;
- const:这个表至多有一个匹配行,
EXPLAIN SELECT
*
FROM
emp
WHERE
empno = 7369;
- system:表只有一行记录(等于系统表),这是const类型的特例,平时不会出现
possible_keys
?显示可能应用在这张表中的索引,一个或多个,查询涉及到的字段上若存在索引,则该索引将被列出,但不一定被查询实际使用。
EXPLAIN SELECT
*
FROM
emp,
dept
WHERE
emp.deptno = dept.deptno
AND emp.deptno = 10;
key
实际使用的索引,如果为null,则没有使用索引,查询中若使用了覆盖索引,则该索引和查询的select字段重叠。
EXPLAIN SELECT
*
FROM
emp,
dept
WHERE
emp.deptno = dept.deptno
AND emp.deptno = 10;
key_len
表示索引中使用的字节数,可以通过key_len计算查询中使用的索引长度,在不损失精度的情况下长度越短越好。
EXPLAIN SELECT
*
FROM
emp,
dept
WHERE
emp.deptno = dept.deptno
AND emp.deptno = 10;
ref
显示索引的哪一列被使用了,如果可能的话,是一个常数
EXPLAIN SELECT
*
FROM
emp,
dept
WHERE
emp.deptno = dept.deptno
AND emp.deptno = 10;
rows
根据表的统计信息及索引使用情况,大致估算出找出所需记录需要读取的行数,此参数很重要,直接反应的sql找了多少数据,在完成目的的情况下越少越好。
EXPLAIN SELECT
*
FROM
emp;
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;