38

如何利用工具,迅猛定位低效SQL? | 1分钟系列

 4 years ago
source link: https://www.tuicool.com/articles/67vqmqF
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

两个工具分析SQL死锁

SQL空值带来的大坑

两个案例分析,展现了MySQL性能分析工具explain的强大。

同一个SQL语句,为啥性能差异咋就这么大呢?

详细叙述了explain结果中最重要的 type字段 (连接类型)的含义。

其实,explain结果中还有一个 Extra字段 ,对分析与优化SQL有很大的帮助,今天花1分钟简单和大家聊一聊。

数据准备

create table user (

id int primary key ,

name varchar(20),

sex varchar(5),

index(name)

)engine=innodb;

insert into user values(1, 'shenjian','no');

insert into user values(2, 'zhangsan','no');

insert into user values(3, ' lisi ', 'yes');

insert into user values(4, ' lisi ', 'no');

数据说明

用户表:id 主键索引 ,name 普通索引 (非唯一),sex 无索引

四行记录:其中name普通索引存在重复记录lisi;

实验目的

通过构造各类SQL语句,对explain的Extra字段进行说明,启发式定位待优化低性能SQL语句。

一、【Using where】

aYVjQjJ.png!web

实验语句

explain select * from user where sex='no';

结果说明

Extra为 Using where 说明, SQL使用了where条件过滤数据

需要注意的是:

(1)返回所有记录的SQL,不使用where条件过滤数据,大概率不符合预期,对于这类SQL往往需要进行优化;

(2)使用了where条件的SQL,并不代表不需要优化,往往需要配合explain结果中的type(连接类型)来综合判断;

画外音: join type在 同一个SQL语句,为啥性能差异咋就这么大呢? 一文中有详细叙述,本文不再展开。

本例虽然Extra字段说明使用了where条件过滤,但type属性是ALL,表示需要扫描全部数据,仍有优化空间。

常见的优化方法为,在where过滤属性上添加索引。

画外音: 本例中,sex字段区分度不高,添加索引对性能提升有限。

二、【Using index】

VfiUfm7.png!web

实验语句

explain select id,name from user where name='shenjian';

结果说明

Extra为 Using index 说明, SQL所需要返回的所有列数据均在一棵索引树上,而无需访问实际的行记录

画外音: The column information is retrieved from the table using only information in the index tree without having to do an additional seek to read the actual row.

这类SQL语句往往性能较好。

问题来了,什么样的列数据,会包含在索引树上呢?

三、【Using index condition】

EBf2Ez6.png!web

实验语句:

explain select id,name,sex from user 

where name='shenjian';

画外音: 该SQL语句与上一个SQL语句不同的地方在于,被查询的列,多了一个sex字段。

结果说明:

Extra为 Using index condition 说明, 确实命中了索引,但不是所有的列数据都在索引树上,还需要 访问实际的行记录

画外音: 聚集索引,普通索引的底层实现差异,详见《 1分钟了解MyISAM与InnoDB的索引差异 》。

这类SQL语句性能也较高,但不如 Using index

问题来了,如何优化为Using index呢?

四、【Using filesort】

yUZni2n.png!web

实验语句:

explain select * from user order by sex;

结果说明:

Extra为 Using filesort 说明, 得到所需结果集,需要对所有记录进行文件排序

这类SQL语句性能极差 ,需要进行优化。

典型的,在一个没有建立索引的列上进行了 order by ,就会触发 filesort ,常见的优化方案是,在 order by 的列上添加索引,避免每次查询都全量排序。

五、【Using temporary】

RNVRzmJ.png!web

实验语句:

explain select * from user group by name order by sex;

结果说明:

Extra为 Using temporary 说明, 需要建立临时表 (temporary table) 来暂存中间结果

这类SQL语句性能较低,往往也需要进行优化。

典型的, group by order by 同时存在,且作用于不同的字段时,就会建立临时表,以便计算出最终的结果集。

六、【Using join buffer (Block Nested Loop)】

7JJzIfB.png!web

实验语句:

explain select * from user where id in(select id from user where sex='no');

结果说明:

Extra为 Using join buffer (Block Nested Loop) 说明, 需要进行嵌套循环计算

画外音: 内层和外层的type均为ALL,rows均为4,需要循环进行4*4次计算。

这类SQL语句性能往往也较低,需要进行优化。

典型的,两个关联表join,关联字段均未建立索引,就会出现这种情况。常见的优化方案是,在关联字段上添加索引,避免每次嵌套循环计算。

结尾

explain是SQL优化中最常用的工具,搞定type和Extra,explain也就基本搞定了。

以上几篇文章,强烈 建议 大家读透。

r6NBFbA.jpg!web

架构师之路 -分享技术思路

相关推荐

缓冲池(buffer pool),这次彻底懂了!

写缓冲(change buffer),这次彻底懂了!

作业

select id,name where XXX Using index

select id,name,sex where XXX Using index condition

后者如何优化为 Using index

希望大家有收获,帮忙 再看 哟。


Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK