Explain 结果解读与实践mysql
基于 MySQL 5.0.67 ,存储引擎 MyISAM 。sql
注:单独一行的"%%"及"`"表示分隔内容,就象分开“第一章”“第二章”。数据库
explain 能够分析 select 语句的执行,即 MySQL 的“执行计划”:服务器
mysql> explain select 1;架构
+----+-------------+-------+------+---------------+------+---------+------+------+----------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+---------------+------+---------+------+------+----------------+
| 1 | SIMPLE | NULL | NULL | NULL | NULL | NULL | NULL | NULL | No tables used |
+----+-------------+-------+------+---------------+------+---------+------+------+----------------+性能
用"\G"代替分号可获得竖排的格式:优化
mysql> explain select 1\G
*************************** 1
id: 1
select_type: SIMPLE
table: NULL
type: NULL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: NULL
Extra: No tables used
spa
`架构设计
能够用 desc 代替 explain :设计
desc select 1;
%%
id 列
建表:
create table a(a_id int);
create table b(b_id int);
create table c(c_id int);
mysql> explain select * from a join b on a_id=b_id where b_id in (select c_id from c);
+----+--------------------+-------+...
| id | select_type | table |...
+----+--------------------+-------+...
| 1 | PRIMARY | a |...
| 1 | PRIMARY | b |...
| 2 | DEPENDENT SUBQUERY | c |...
+----+--------------------+-------+...
从 3 个表中查询,对应输出 3 行,每行对应一个表, id 列表示执行顺序,id 越大,越先执行,id 相同,由上至下执行。此处的执行顺序为(以 table 列表示):c -> a -> b
%%
select_type 列
MySQL 把 SELECT 查询分红简单和复杂两种类型,复杂类型又能够分红三个大类:简单子查询、所谓的衍生表(子查询在 FROM 子句里)和 UNION 。
SIMPLE:查询中不包含子查询或者UNION
mysql> explain select * from a;
+----+-------------+-------+...
| id | select_type | table |...
+----+-------------+-------+...
| 1 | SIMPLE | a |...
+----+-------------+-------+...
SUBQUERY:子查询
PRIMARY:子查询上层
mysql> explain select * from a where a_id in (select b_id from b);
+----+--------------------+-------+...
| id | select_type | table |...
+----+--------------------+-------+...
| 1 | PRIMARY | a |...
| 2 | DEPENDENT SUBQUERY | b |...
+----+--------------------+-------+...
DERIVED:在FROM列表中包含子查询, MySQL 会递归执行这些子查询,把结果放在临时表里。
mysql> explain select count(*) from (select * from a) as der;
+----+-------------+-------+...
| id | select_type | table |...
+----+-------------+-------+...
| 1 | PRIMARY | NULL |...
| 2 | DERIVED | a |...
+----+-------------+-------+...
%%
table 列
显示每行对应的表名。若在 SELECT 语句中为表起了别名,则会显示表的别名。
一个很复杂的示例及解释可参考《高性能 MySQL 》(第二版)中文版 P467(pdf.491) 〈附录 B.2.3 table 列〉
%%
type 列
MySQL 在表里找到所需行的方式。包括(由左至右,由最差到最好):
| All | index | range | ref | eq_ref | const,system | null |
ALL
全表扫描,MySQL 从头至尾扫描整张表查找行。
mysql> explain select * from a\G
...
type: ALL
若是加上 limit 如 select * from a limit 10 MySQL 会扫描 10 行,但扫描方式不会变,仍是从头至尾扫描。
`
index
按索引次序扫描表,就是先读索引,再读实际的行,其实仍是全表扫描。主要优势是避免了排序,由于索引是排好序的。
建表:
create table a(a_id int not null, key(a_id));
insert into a value(1),(2)
mysql> explain select * from a\G
...
type: index
`
range
以范围的形式扫描索引
建表:
create table a(a_id int not null, key(a_id));
insert into a values(1),(2),(3),(4),(5),(6),(7),(8),(9),(10);
mysql> explain select * from a where a_id > 1\G
...
type: range
...
IN 比较符也会用 range 表示:
mysql> explain select * from a where a_id in (1,3,4)\G
...
type: range
...
`
ref
非惟一性索引访问
建表:
create table a(a_id int not null, key(a_id));
insert into a values(1),(2),(3),(4),(5),(6),(7),(8),(9),(10);
mysql> explain select * from a where a_id=1\G
...
type: ref
...
`
eq_ref
使用有惟一性索引查找(主键或惟一性索引)
建表及插入数据:
create table a(id int primary key);
create table a_info(id int primary key, title char(1));
insert into a value(1),(2);
insert into a_info value(1, 'a'),(2, 'b');
mysql> explain select * from a join a_info using(id);
...+--------+--------+...
...| table | type |...
...+--------+--------+...
...| a | index |...
...| a_info | eq_ref |...
...+--------+--------+...
此时 a_info 每条记录与 a 一一对应,经过主键 id 关联起来,因此 a_info 的 type 为 eq_ref。
删除 a_info 的主键:ALTER TABLE `a_info` DROP PRIMARY KEY;
如今 a_info 已经没有索引了:
mysql> explain select * from a join a_info using(id);
+----+...+--------+--------+...
| id |...| table | type |...
+----+...+--------+--------+...
| 1 |...| a_info | ALL |...
| 1 |...| a | eq_ref |...
+----+...+--------+--------+...
此次 MySQL 调整了执行顺序,先全表扫描 a_info 表,再对表 a 进行 eq_ref 查找,由于 a 表 id 仍是主键。
删除 a 的主键:alter table a drop primary key;
如今 a 也没有索引了:
mysql> explain select * from a join a_info using(id);
...+--------+------+...
...| table | type |...
...+--------+------+...
...| a | ALL |...
...| a_info | ALL |...
...+--------+------+...
如今两个表都使用全表扫描了。
建表及插入数据:
create table a(id int primary key);
create table a_info(id int, title char(1), key(id));
insert into a value(1),(2);
insert into a_info value(1, 'a'),(2, 'b');
如今 a_info 表 id 列变为普通索引(非惟一性索引):
mysql> explain select * from a join a_info using(id) where a.id=1;
...+--------+-------+...
...| table | type |...
...+--------+-------+...
...| a | const |...
...| a_info | ref |...
...+--------+-------+...
a_info 表 type 变为 ref 类型了。
因此,惟一性索引才会出现 eq_ref (非惟一性索引会出现 ref ),由于惟一,因此最多只返回一条记录,找到后无需继续查找,所以比 ref 更快。
`
const
被称为“常量”,这个词很差理解,不过出现 const 的话就表示发生下面两种状况:
在整个查询过程当中这个表最多只会有一条匹配的行,好比主键 id=1 就确定只有一行,只需读取一次表数据便能取得所需的结果,且表数据在分解执行计划时读取。
返回值直接放在 select 语句中,相似 select 1 AS f 。能够经过 extended 选择查看内部过程:
建表及插入数据:
create table a(id int primary key, c1 char(20) not null, c2 text not null, c3 text not null);
insert into a values(1, 'asdfasdf', 'asdfasdf', 'asdfasdf'), (2, 'asdfasdf', 'asdfasdf', 'asdfasdf');
mysql> explain extended select * from a where id=1\G
...
type: const
possible_keys: PRIMARY
key: PRIMARY
...
用 show warnings 查看 MySQL 是如何优化的:
mysql> show warnings\G
...
Message: select '1' AS `id`,'asdfasdf' AS `c1`,'asdfasdf' AS `c2`,'asdfasdf' AS
`c3` from `test`.`a` where 1
查询返回的结果为:
mysql> select * from a where id=1;
+----+----------+----------+----------+
| id | c1 | c2 | c3 |
+----+----------+----------+----------+
| 1 | asdfasdf | asdfasdf | asdfasdf |
+----+----------+----------+----------+
能够看出,返回结果中的字段值都以“值 AS 字段名”的形式直接出如今优化后的 select 语句中。
修改一下查询:
mysql> explain select * from a where id in(1,2)\G
...
type: range
...
当返回结果超过 1 条时, type 便再也不为 const 了。
从新建表及插入数据:
create table a (id int not null);
insert into a value(1),(2),(3);
mysql> explain select * from a where id=1\G
...
type: ALL
目前表中只有一条 id=1 的记录,但 type 已为 ALL ,由于只有惟一性索引才能保证表中最多只有一条记录,只有这样 type 才有可能为 const 。
为 id 加普通索引后, type 变为 ref ,改成加惟一或主键索引后, type 便变为 const 了。
相关阅读:
《高性能 MySQL 》第 2 版中文版
P130(pdf.153) 计算和减小常量表达式
P471(pdf.495) B.2.4 type 列
《 MySQL 性能调优与架构设计》
pdf.109
MySQL 帮助手册 7.2.1. EXPLAIN语法(获取SELECT相关信息)
`
system
system 是 const 类型的特例,当表只有一行时就会出现 system 。
建表及插入数据:
create table a(id int primary key);
insert into a value(1);
mysql> explain select * from a\G
...
type: system
...
`
NULL
在优化过程当中就已获得结果,不用再访问表或索引。
mysql> explain select min(a_id) from a\G
...
type: NULL
...
%%
possible_keys 列
可能被用到的索引
建表:
create table a (a_id int primary key, a_age int, key (a_id, a_age));
此表有 主键及普通索引 两个索引。
mysql> explain select * from a where a_id=1\G
...
possible_keys: PRIMARY,a_id
%%
key 列
查询过程当中实际使用的索引
mysql> explain select * from a where a_id=1\G
...
possible_keys: PRIMARY,a_id
key: PRIMARY
%%
key_len 列
索引字段最大可能使用的长度。
mysql> explain select * from a where a_id=1\G
...
key: PRIMARY
key_len: 4
a_id 是 int 类型,int 的长度是 4 字节,因此 key_len 为 4。
%%
ref 列
指出对 key 列所选择的索引的查找方式,常见的值有 const, func, NULL, 具体字段名。当 key 列为 NULL ,即不使用索引时,此值也相应的为 NULL 。
建表及插入数据:
create table a(id int primary key, age int);
insert into a value(1, 10),(2, 10);
mysql> desc select * from a where age=10\G
...
key: NULL
key_len: NULL
ref: NULL
...
当 key 列为 NULL , ref 列也相应为 NULL 。
mysql> explain select * from a where id=1\G
...
key: PRIMARY
key_len: 4
ref: const
此次 key 列使用了主键索引,where id=1 中 1 为常量, ref 列的 const 即是指这种常量。
mysql> explain select * from a where id in (1,2)\G
...
possible_keys: PRIMARY
key: PRIMARY
key_len: 4
ref: NULL
...
不理解 ref 为 NULL 的含意,好比上面这个查询, key 列有使用索引,但 ref 列却为 NULL 。网上搜索及查阅了一下 MySQL 帮助手册都没有找到相关的描述。
再建表及插入数据:
create table a(id int primary key, a_name int not null);
create table b(id int primary key, b_name int not null);
insert into a value(1, 1),(2, 2),(3, 3);
insert into b value(1, 111),(2, 222),(3, 333);
mysql> explain select * from a join b using(id);
...+-------+--------+...+---------+...+-----------+...
...| table | type |...| key |...| ref |...
...+-------+--------+...+---------+...+-----------+...
...| a | ALL |...| NULL |...| NULL |...
...| b | eq_ref |...| PRIMARY |...| test.a.id |...
...+-------+--------+...+---------+...+-----------+...
这里 test.a.id 即为具体字段,意为根据表 a 的 id 字段的值查找表 b 的主键索引。
mysql> explain select * from a join b using(id) where b.id=1;
...+-------+-------+...+---------+...+-------+...
...| table | type |...| key |...| ref |...
...+-------+-------+...+---------+...+-------+...
...| a | const |...| PRIMARY |...| const |...
...| b | const |...| PRIMARY |...| const |...
...+-------+-------+...+---------+...+-------+...
由于 a join b 的条件为 id 相等,而 b.id=1 ,就是 a.id 也为 1 ,因此 a,b 两个表的 ref 列都为 const 。
ref 为 func 的状况出如今子查询中,暂不明其原理:
mysql> explain select * from a where id in (select id from b where id in (1,2));
+----+--------------------+-------+...+---------+...+------+...
| id | select_type | table |...| key |...| ref |...
+----+--------------------+-------+...+---------+...+------+...
| 1 | PRIMARY | a |...| NULL |...| NULL |...
| 2 | DEPENDENT SUBQUERY | b |...| PRIMARY |...| func |...
+----+--------------------+-------+...+---------+...+------+...
%%
rows 列
MySQL 估计的须要扫描的行数。只是一个估计。
%%
Extra 列
显示上述信息以外的其它信息,但却很重要。
Using index
此查询使用了覆盖索引(Covering Index),即经过索引就能返回结果,无需访问表。
若没显示"Using index"表示读取了表数据。
建表及插入数据:
create table a (id int primary key, age int);
insert into a value(1, 10),(2, 10);
mysql> explain select id from a\G
...
Extra: Using index
由于 id 为主键索引,索引中直接包含了 id 的值,因此无需访问表,直接查找索引就能返回结果。
mysql> explain select age from a\G
...
Extra:
age 列没有索引,所以没有 Using index ,意即须要访问表。
为 age 列添加索引:create index age on a(id, age);
mysql> explain select age from a\G
...
Extra: Using index
如今索引 age 中也包含了 age 列的值,所以不用访问表便能返回结果了。
建表:create table a(id int auto_increment primary key, age int, name char(10));
插入 100w 条数据:insert into a value(null, rand()*100000000, 'jack');
`
Using where
表示 MySQL 服务器从存储引擎收到行后再进行“后过滤”(Post-filter)。所谓“后过滤”,就是先读取整行数据,再检查此行是否符合 where 句的条件,符合就留下,不符合便丢弃。由于检查是在读取行后才进行的,因此称为“后过滤”。
建表及插入数据:
create table a (num_a int not null, num_b int not null, key(num_a));
insert into a value(1,1),(1,2),(2,1),(2,2);
mysql> explain select * from a where num_a=1\G
...
type: ref
possible_keys: num_a
key: num_a
key_len: 4
...
Extra:
虽然查询中有 where 子句,但只有 num_a=1 一个条件,且 num_a 列存在索引,经过索引便能肯定返回的行,无需进行“后过滤”。
因此,并不是带 WHERE 子句就会显示"Using where"的。
mysql> explain select * from a where num_a=1 and num_b=1\G
...
type: ref
possible_keys: num_a
key: num_a
...
Extra: Using where
此查询增长了条件 num_b=1 ,此列没有索引,但能够看到查询一样能使用 num_a 索引。 MySQL 先经过索引 num_a 找到 num_a=1 的行,而后读取整行数据,再检查 num_b 是否等于 1 ,执行过程看上去象这样:
num_a索引|num_b 没有索引,属于行数据
+-------+-------+
| num_a | num_b | where 子句(num_b=1)
+-------+-------+
| 1 | 1 | 符合
| 1 | 2 | 不符合
| ... | ... | ...
+-------+-------+
在《高性能 MySQL 》(第二版)P144(pdf.167) 页有更形象的说明图片(图 4-5 MySQL 经过整表扫描查找数据)。
字段是否容许 NULL 对 Using where 的影响:
建表及插入数据:
create table a (num_a int null, num_b int null, key(num_a));
insert into a value(1,1),(1,2),(2,1),(2,2);
此次 num_a, num_b 字段容许为空。
在上例 num_a not null 时, num_a 索引的长度 key_len 为 4 ,当 num_a null 时, num_a 索引的长度变为了 5 :
mysql> explain select * from a where num_a=1\G
...
type: ref
possible_keys: num_a
key: num_a
key_len: 5
...
Extra: Using where
而且哪怕只有 num_a=1 一个条件,也会出现 Using where 。缘由暂不明白。
`
Using temporary
使用到临时表
建表及插入数据:
create table a(a_id int, b_id int);
insert into a values(1,1),(1,1),(2,1),(2,2),(3,1);
mysql> explain select distinct a_id from a\G
...
Extra: Using temporary
MySQL 使用临时表来实现 distinct 操做。
`
Using filesort
若查询所需的排序与使用的索引的排序一致,由于索引是已排序的,所以按索引的顺序读取结果返回,不然,在取得结果后,还须要按查询所需的顺序对结果进行排序,这时就会出现 Using filesort 。
建表及插入数据:
create table a(a_id int, b_id int);
insert into a values(1,1),(1,1),(2,1),(2,2),(3,1);
mysql> explain select * from a order by a_id\G
...
Extra: Using filesort
对于没有索引的表,只要 order by 必会出现 Using filesort 。
如今增长索引:create index a_id on a(a_id);
把表 a 的记录增长到约 100w(1048576) 条, a_id 与 b_id 都是随机生成的数字:
mysql> select * from a order by rand() limit 10;
+-------+--------+
| a_id | b_id |
+-------+--------+
| 61566 | 961297 |
| 33951 | 680542 |
| ..... | ...... |
+-------+--------+
mysql> explain select * from a order by a_id\G
...
type: ALL
...
rows: 1048576
Extra: Using filesort
一样是 Using filesort ,type 为 ALL ,全表扫描。据说“取全表数据根据ID排序,走索引必定不如直接查,由于能够减小由于须要索引改变数据访问顺序形成随机IO的几率,数据库放弃索引是应该的”,参考:
http://isky000.com/database/mysql_order_by_implement#comment-2981
当 type 为 rang、 ref 或者 index 的时候才有可能利用索引排序,其它,如 ALL ,都没法经过索引排序,此时如有 order by ,如上例,便会出现 Using filesort 。
如今增长 where 子句:
mysql> explain select * from a where a_id=10 order by a_id\G
...
type: ref
possible_keys: a_id
key: a_id
...
rows: 8
Extra:
查询走了索引 a_id ,此时 type 为 ref ,直接按索引顺序返回,没有 Using filesort 。
修改 where 子句:
mysql> explain select * from a where a_id>10 and a_id<100 order by a_id\G
...
type: range
possible_keys: a_id
key: a_id
...
rows: 712
Extra: Using where
一样利用索引排序,没有 Using filesort 。
再修改 where 子句:
mysql> explain select * from a where a_id >10 order by a_id\G
...
type: ALL
possible_keys: a_id
key: NULL
...
rows: 1048576
Extra: Using where; Using filesort
又出现 Using filesort 且 type 变为 ALL 。注意以上例子的 rows 列,此列表示 MySQL 估计查询须要读取的行数,分别为 1048576, 8, 712, 1048576 ,特别注意最后两个数字: 712, 1048576 。
可见,当索引能为查询排除大部份行时( a_id=10 时约读取 8 行,排除了大部份, a_id>10 and a_id<100 时约读取 712 行,一样排除了大部份)便使用索引,不然,如 a_id>10 时约读取 1048576 , MySQL 直接改用全表扫描,再 Using filesort 。也就是说, MySQL 会根据表中的信息及查询来决定使用任种方式。
关于 MySQL 读取数据表的方式,可参考(暂缺参考资料),就会明白为何需读取 1048576 行时,先读索引再读表数据还不如全表扫描了。
对于多字段排序(order by a, b)及带 group by 的查询,可参考 MySQL 帮助手册 7.2.12. MySQL如何优化ORDER BY 。