各位用户为了找寻关于Mysql调优Explain工具详解及实战演练(推荐)的资料费劲了很多周折。这里教程网为您整理了关于Mysql调优Explain工具详解及实战演练(推荐)的相关资料,仅供查阅,以下为您介绍关于Mysql调优Explain工具详解及实战演练(推荐)的详细内容
mysql调优explain工具详解以及实战演练 explain工具介绍explain分析示例explain 两个变种explain中的列 索引最佳实战索引使用总结: mysql安装文档参考
explain工具介绍
使用explain关键字可以模拟优化器执行sql语句,分析你的查询语句或是结构的性能瓶颈 在 select 语句之前增加 explain 关键字,mysql 会在查询上设置一个标记,执行查询会返回执行计划的信息,而不是 执行这条sql 注意:如果 from 中包含子查询,仍会执行该子查询,将结果放入临时表中
explain分析示例
参考官方文档
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30示例表:
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);
1
mysql> explain
select
*
from
actor;
在查询中的每个表会输出一行,如果有两个表通过 join 连接查询,那么会输出两行
explain 两个变种
1)explain extended:会在 explain 的基础上额外提供一些查询优化的信息。紧随其后通过 show warnings 命令可 以得到优化后的查询语句,从而看出优化器优化了什么。额外还有 filtered 列,是一个半分比的值,rows * filtered/100 可以估算出将要和 explain 中前一个表进行连接的行数(前一个表指 explain 中的id值比当前表id值小的 表)。
2)explain partitions:相比 explain 多了个 partitions 字段,如果查询是基于分区表的话,会显示查询将访问的分 区。
在新版版比如mysql5.7以上的版本,并不需要携带extended就可以查询出来额外的信息,在mysql8.0以上已经废除了explain extended这条命令,我们只需要使用explain就可以了。
explain中的列
接下来我们将展示 explain 中每个列的信息。
id列 id列的编号是 select 的序列号,有几个 select 就有几个id,并且id的顺序是按 select 出现的顺序增长的。 id列越大执行优先级越高,id相同则从上往下执行,id为null最后执行。 如果查询有连接查询,id出现了多个。比如说1,2,3,那么id为3的这条sql最先执行,如果查询出来的id为1,1,2,3,那么两个id都为1,则在上面的sql先执行。
select_type列 select_type 表示对应行是简单还是复杂的查询。 1).simple:简单查询。查询不包含子查询和union
1mysql> explain
select
*
from
film
where
id = 2;
2).primary:复杂查询中最外层的 select 3).subquery:包含在 select 中的子查询(不在 from 子句中) 4).derived:包含在 from 子句中的子查询。mysql会将结果存放在一个临时表中,也称为派生表(derived的英文含 义) 用这个例子来了解 primary、subquery 和 derived 类型
1 2mysql>
set
session optimizer_switch=
'derived_merge=off'
; #关闭mysql5.7新特性对衍生表的合 并优化
2 explain
select
(
select
1
from
actor
where
id = 1)
from
(
select
*
from
film
where
id = 1) der;
解释一下上面的select_type查询到的图, 首先说一下id为3的sql优先执行,因为它是在from后面的子查询,所以对应的select_type为derived,id为2后sql后执行,因为它包含在 select 中的子查询(不在 from 子句中),所以对应的select_type为subquery, id为1 的sql最后执行,它是复杂查询中最外层的 select,所以对应的select_type为primary。 最后别忘了把前面修改的配置还原:
1mysql>
set
session optimizer_switch=
'derived_merge=on'
; #还原默认配置
5).union:在 union 中的第二个和随后的 select
1mysql> explain
select
1
union
all
select
1;
3.table列
这一列表示 explain 的一行正在访问哪个表。 当 from 子句中有子查询时,table列是 格式,表示当前查询依赖 id=n 的查询,于是先执行 id=n 的查 询。 当有 union 时,union result 的 table 列的值为<union1,2>,1和2表示参与 union 的 select 行id。 deriven3,就表示先查询id为3的sql,表示当前查询依赖 id=3 的查询
4.type列(比较重要)
这一列表示关联类型或访问类型,即mysql决定如何查找表中的行,查找数据行记录的大概范围。 依次从最优到最差分别为:system > const > eq_ref > ref > range > index > all 一般来说,得保证查询达到range级别,最好达到ref null: mysql能够在优化阶段分解查询语句,在执行阶段用不着再访问表或索引。例如:在索引列中选取最小值,可 以单独查找索引来完成,不需要在执行时访问表。
1mysql> explain
select
min
(id)
from
film;
在这里解释一下,因为mysql底层索引数据结构式b+树,在上一篇文章中已经重要解释过。在b+树最下面的叶子节点所以是按照顺序排列的,从左到右依次递增,这个也就是最左前缀原则,那么查询最小的数值,直接可以到索引最左边拿到就可以,不需要查询,这样的效率是非常高的。
const, system: mysql能对查询的某部分进行优化并将其转化成一个常量(可以看show warnings 的结果)。用于 primary key 或 unique key 的所有列与常数比较时,所以表最多有一个匹配行,读取1次,速度比较快。system是 const的特例,表里只有一条元组匹配时为system。
1mysql> explain extended
select
*
from
(
select
*
from
film
where
id = 1) tmp;
mysql> show warnings;
eq_ref:primary key 或 unique key 索引的所有部分被连接使用 ,最多只会返回一条符合条件的记录。这可能是在 const 之外最好的联接类型了,简单的 select 查询不会出现这种 type。
1explain
select
*
from
film_actor
left
join
film
on
film_actor.film_id = film.id;
解释一下,上面的film_actor字段film_id为联合索引,所以根据二级索引对应另一张表的聚集索引查询是非常快的 ref: 相比 eq_ref,不使用唯一索引,而是使用普通索引或者唯一性索引的部分前缀,索引要和某个值相比较,可能会 找到多个符合条件的行。 1.简单 select 查询,name是普通索引(非唯一索引)
1mysql> explain
select
*
from
film
where
name
=
'film1'
;
2. 关联表查询,idx_film_actor_id是film_id和actor_id的联合索引,这里使用到了film_actor的左边前缀film_id部分。
1mysql>explain
select
film_id
from
film
left
join
film_actor
on
film.id = film_actor.film_id;
range:范围扫描通常出现在 in(), between ,> ,<, >= 等操作中。使用一个索引来检索给定范围的行
1mysql> explain
select
*
from
actor
where
id > 1;
index:扫描全索引就能拿到结果,一般是扫描某个二级索引,这种扫描不会从索引树根节点开始快速查找,而是直接 对二级索引的叶子节点遍历和扫描,速度还是比较慢的,这种查询一般为使用覆盖索引,二级索引一般比较小,所以这 种通常比all快一些。
1mysql> explain
select
*
from
film;
解释一下为什么说二级索引比聚集索引要小,因为二级索引只保存了当前索引的数据,而聚集索引保存的是全部表数据。 all:即全表扫描,扫描你的聚簇索引的所有叶子节点。通常情况下这需要增加索引来进行优化了
5.possible_keys列
这一列显示查询可能使用哪些索引来查找。 explain 时可能出现 possible_keys 有列,而 key 显示 null 的情况,这种情况是因为表中数据不多,mysql认为索引 对此查询帮助不大,选择了全表查询。 如果该列是null,则没有相关的索引。在这种情况下,可以通过检查 where 子句看是否可以创造一个适当的索引来提 高查询性能,然后用 explain 查看效果。
6.key列
这一列显示mysql实际采用哪个索引来优化对该表的访问。 如果没有使用索引,则该列是 null。如果想强制mysql使用或忽视possible_keys列中的索引,在查询中使用 force index、ignore index。
7.key_len列
这一列显示了mysql在索引里使用的字节数,通过这个值可以算出具体使用了索引中的哪些列。 举例来说,film_actor的联合索引 idx_film_actor_id 由 film_id 和 actor_id 两个int列组成,并且每个int是4字节。通 过结果中的key_len=4可推断出查询使用了第一个列:film_id列来执行索引查找。
1mysql> explain
select
*
from
film_actor
where
film_id = 2;
key_len计算规则如下:
字符串,char(n)和varchar(n),5.0.3以后版本中,n均代表字符数,而不是字节数,如果是utf-8,一个数字 或字母占1个字节,一个汉字占3个字节 char(n):如果存汉字长度就是 3n 字节 varchar(n):如果存汉字则长度是 3n + 2 字节,加的2字节用来存储字符串长度,因为 varchar是变长字符串 数值类型 tinyint:1字节 smallint:2字节 int:4字节 bigint:8字节 时间类型 date:3字节 timestamp:4字节 datetime:8字节 如果字段允许为 null,需要1字节记录是否为 null 索引最大长度是768字节,当字符串过长时,mysql会做一个类似左前缀索引的处理,将前半部分的字符提取出来做索 引。8.ref列
这一列显示了在key列记录的索引中,表查找值所用到的列或常量,常见的有:const(常量),字段名(例:film.id)
9.rows列
这一列是mysql估计要读取并检测的行数,注意这个不是结果集里的行数。
10extra列
这一列展示的是额外信息。常见的重要值如下: 1)using index:使用覆盖索引 覆盖索引定义:mysql执行计划explain结果里的key有使用索引,如果select后面查询的字段都可以从这个索引的树中 获取,这种情况一般可以说是用到了覆盖索引,extra里一般都有using index;覆盖索引一般针对的是辅助索引,整个 查询结果只通过辅助索引就能拿到结果,不需要通过辅助索引树找到主键,再通过主键去主键索引树里获取其它字段值。 简单点说就是不用回表,通过二级索引也就是联合索引就可以拿到想要的结果集,
1mysql> explain
select
film_id
from
film_actor
where
film_id = 1;
2)using where:使用 where 语句来处理结果,并且查询的列未被索引覆盖
1mysql> explain
select
*
from
actor
where
name
=
'a'
;
这里的actor表的name是没有添加索引的。 3)using index condition:查询的列不完全被索引覆盖,where条件中是一个前导列的范围; 4)using temporary:mysql需要创建一张临时表来处理查询。出现这种情况一般是要进行优化的,首先是想到用索 引来优化。
其实还有很多,就不一一介绍了,有兴趣的可以自己查看mysql官方文档。
索引最佳实战
1 2 3 4 5 6 7 8 9 10 11 12 13 14示例表:
create
table
`employees` (
`id`
int
(11)
not
null
auto_increment,
`
name
`
varchar
(24)
not
null
default
''
comment
'姓名'
,
`age`
int
(11)
not
null
default
'0'
comment
'年龄'
,
`position`
varchar
(20)
not
null
default
''
comment
'职位'
,
`hire_time`
timestamp
not
null
default
current_timestamp
comment
'入职时间'
,
primary
key
(`id`),
key
`idx_name_age_position` (`
name
`,`age`,`position`) using btree
) engine=innodb auto_increment=4
default
charset=utf8 comment=
'员工记录表'
;
insert
into
employees(
name
,age,position,hire_time)
values
(
'lilei'
,22,
'manager'
,now());
insert
into
employees(
name
,age,position,hire_time)
values
(
'hanmeimei'
, 23,
'dev'
,now());
insert
into
employees(
name
,age,position,hire_time)
values
(
'lucy'
,23,
'dev'
,now());
全值匹配
1explain
select
*
from
employees
where
name
=
'lilei'
;
explain
select
*
from
employees
where
name
=
'lilei'
and
age = 22;
explain
select
*
from
employees
where
name
=
'lilei'
and
age = 22
and
position =
'manage r'
;
2. 最左前缀法则 如果索引了多列,要遵守最左前缀法则。指的是查询从索引的最左前列开始并且不跳过索引中的列。
1 2 3explain
select
*
from
employees
where
name
=
'bill'
and
age = 31;
explain
select
*
from
employees
where
age = 30
and
position =
'dev'
;
explain
select
*
from
employees
where
position =
'manager'
;
上面会有三个结果集,只有第一条sql遵循了最左前缀原则,使用了索引进行查询,另外两条sql都是违背了最左前缀原则,就是没有从name字段开始查询,所以没有使用索引,导致索引失效。 3. 不在索引列上做任何操作(计算、函数、(自动or手动)类型转换),会导致索引失效而转向全表扫描
1 2explain
select
*
from
employees
where
name
=
'lilei'
;
explain
select
*
from
employees
where
left
(
name
,3) =
'lilei'
;
第一条sql使用索引,第二条sql导致了索引失效。 4. 存储引擎不能使用索引中范围条件右边的列
1 2explain
select
*
from
employees
where
name
=
'lilei'
and
age = 22
and
position =
'manage r'
;
2 explain
select
*
from
employees
where
name
=
'lilei'
and
age > 22
and
position =
'manage r'
;
varchar(n):如果存汉字则长度是 3n + 2 字节,加的2字节用来存储字符串长度,因为 varchar是变长字符串 name字段所占字节:3*24+2=74 age字段为int占4个字节 74+4=78 这样就会导致后面的position索引失效 5. 尽量使用覆盖索引(只访问索引的查询(索引列包含查询列)),减少 select * 语句
1explain
select
name
,age
from
employees
where
name
=
'lilei'
and
age = 23
and
position =
'manager'
;
6. mysql在使用不等于(!=或者<>),not in ,not exists 的时候无法使用索引会导致全表扫描 < 小于、 > 大于、 <=、>= 这些,mysql内部优化器会根据检索比例、表大小等多个因素整体评估是否使用索引
is null,is not null 一般情况下也无法使用索引
1explain
select
*
from
employees
where
name
is
null
like以通配符开头('$abc…')mysql索引失效会变成全表扫描操作 问题:解决like'%字符串%'索引不被使用的方法 a)使用覆盖索引,查询字段必须是建立覆盖索引字段
1explain
select
name
,age,position
from
employees
where
name
like
'%lei%'
;
b)如果不能使用覆盖索引则可能需要借助搜索引擎 9. 字符串不加单引号索引失效
1 2explain
select
*
from
employees
where
name
=
'1000'
;
explain
select
*
from
employees
where
name
= 1000;
少用or或in,用它查询时,mysql不一定使用索引,mysql内部优化器会根据检索比例、表大小等多个因素整体评 估是否使用索引,详见范围查询优化范围查询优化 给年龄添加单值索引
1 2alter
table
`employees`
add
index
`idx_age` (`age`) using btree ;
explain
select
*
from
employees
where
age >=1
and
age <=2000;
没走索引原因:mysql内部优化器会根据检索比例、表大小等多个因素整体评估是否使用索引。比如这个例子,可能是 由于单次数据量查询过大导致优化器最终选择不走索引 优化方法:可以将大的范围拆分成多个小范围
1 2explain
select
*
from
employees
where
age >=1
and
age <=1000;
explain
select
*
from
employees
where
age >=1001
and
age <=2000;
还原最初索引状态
1alter
table
`employees`
drop
index
`idx_age`;
索引使用总结:
到此这篇关于mysql调优explain工具详解及实战演练的文章就介绍到这了,更多相关mysql调优explain工具内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
原文链接:https://blog.csdn.net/weixin_43691942/article/details/114490917