①. 使用EXPLAIN关键字可以模拟优化器执行SQL查询语句,从而知道MYSQL是如何处理你的SQL语句。分析你的查询语句或是表结构的性能瓶颈
②. 能干什么?
③. 官方参考文档
⑤. 我们可以使用explain extended查询出filtered字段,使用explain partitions查询出partitions字段
# 演员表的name是一个索引字段
DROP TABLE IF EXISTS `actor`;
CREATE TABLE `actor` (
`id` int(11) NOT NULL,
`name` varchar(45) DEFAULT NULL,
`update_time` datetime DEFAULT NULL,
PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `actor` (`id`, `name`, `update_time`) VALUES
(1,'a','2017‐12‐22 15:27:18'),
(2,'b','2017‐12‐22 15:27:18'),
(3,'c','2017‐12‐22 15:27:18');
DROP TABLE IF EXISTS `film`;
CREATE TABLE `film` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(10) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `idx_name` (`name`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `film` (`id`, `name`) VALUES (3,'film0'),(1,'film1'),(2,'film2');
DROP TABLE IF EXISTS `film_actor`;
CREATE TABLE `film_actor` (
`id` int(11) NOT NULL,
`film_id` int(11) NOT NULL,
`actor_id` int(11) NOT NULL,
`remark` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`), KEY `idx_film_actor_id` (`film_id`,`actor_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `film_actor` (`id`, `film_id`, `actor_id`) VALUES (1,1,1),(2,1,2),(3,2,1);
①. id列的编号是select的序列号,有几个select就有几个id,下面将分析三种情况
①. select_type:表示对应行是简单还是复杂的查询。
②. simple:简单查询。查询不包含子查询和union
mysql> explain select * from film where id = 2;
③. primary:复杂查询中最外层的select
④. subquery:包含在select中的子查询(不在from子句中)
(在select后面from前面的叫子查询,在from后面的叫衍生查询)
⑤. derived:包含在from子句中的子查询。MySQL会将结果存放在一个临时表中,也称为派生表(derived的英文含义)
# 关闭mysql5.7新特性对衍生表的合并优化
mysql> set session optimizer_switch='derived_merge=off';
mysql> explain select (select 1 from actor where id = 1) from
(select * from film where id = 1) der;
#还原默认配置
mysql> set session optimizer_switch='derived_merge=on';
⑥. UNION:若第二个SELECT出现在UNION之后,则别标记为UNION
⑦. UNION RESULT:从UNION表获取结果的SELECT
⑧. table:显示这一行的数据是关于哪张表
①. 这一列表示关联类型或访问类型,即MySQL决定如何查找表中的行,查找数据行记录的大概范围
②. 依次从最优到最差分别为:system > const > eq_ref > ref > range > index > ALL
一般来说,得保证查询达到range级别,最好达到ref
③. NULL:mysql能够在优化阶段分解查询语句,在执行阶段用不着再访问表或索引。例如:在索引列中选取最小值,可 以单独查找索引来完成,不需要在执行时访问表
mysql> explain select min(id) from film;
mysql> explain extended select * from (select * from film where id = 1) tmp;
show warnings;
mysql> explain select * from film_actor left join film on film_actor.film_id = film.id;
# 简单select查询,name是普通索引(非唯一索引)
mysql> explain select * from film where name = 'film1';
# 关联表查询,idx_film_actor_id是film_id和actor_id的联合索引,这里使用到了film_actor的左边前缀film_id部分
mysql> explain select film_id from film left join film_actor on film.id = film_actor.fi lm_id;
mysql> explain select * from actor where id > 1;
mysql> explain select * from film;
mysql> explain select * from actor;
①. possible_keys列:这一列显示查询可能使用哪些索引来查找。
②. key列:这一列显示mysql实际采用哪个索引来优化对该表的访问。
下面的意思是:可能使用了主键索引、实际也是用的主键索引
mysql> explain select * from actor where id > 1;
mysql> explain select * from film_actor where film_id = 2;
①. ref字段:这一列显示了在key列记录的索引中,表查找值所用到的列或常量,常见的有:const(常量),字段名(例:film.id)
②. rows字段:这一列是mysql估计要读取并检测的行数,注意这个不是结果集里的行数
mysql> explain select film_id from film_actor where film_id = 1;
mysql> explain select * from actor where name = 'a';
mysql> explain select * from film_actor where film_id > 1;
# 1. actor.name没有索引,此时创建了张临时表来distinct
mysql> explain select distinct name from actor;
#2. film.name建立了idx_name索引,此时查询时extra是using index,没有用临时表
mysql> explain select distinct name from film;
# 1. actor.name未创建索引,会浏览actor整个表,保存排序关键字name和对应的id,然后排序name并检索行记录
mysql> explain select * from actor order by name;
# 2. film.name建立了idx_name索引,此时查询时extra是using index
mysql> explain select * from film order by name;
mysql> explain select min(id) from film;