SQLAdvisor美团SQL索引优化建议工具
SQLAdvisor美团SQL索引优化建议工具
前言
Part1:写在最前
SQLAdvisor是美团开源的一款SQL索引优化建议工具,是由美团点评公司技术工程部DBA团队(北京)开发维护的一个分析SQL给出索引优化建议的工具。它基于MySQL原生态词法解析,结合分析SQL中的where条件、聚合条件、多表Join关系 给出索引优化建议。目前SQLAdvisor在美团点评内部广泛应用,公司内部对SQLAdvisor的开发全面转到github上,开源和内部使用保持一致。本文记录对该工具的初步安装和基本使用。
安装
Part1:构建安装环境
[root@HE3 ~]# yum install git
[root@HE3 ~]# git clone https://github.com/Meituan-Dianping/SQLAdvisor.git
[root@HE3 ~]# yum install cmake libaio-devel libffi-devel glib2 glib2-devel
[root@HE3 ~]# yum install http://www.percona.com/downloads/percona-release/redhat/0.1-3/percona-release-0.1-3.noarch.rpm
[root@HE3 ~]# yum install Percona-Server-shared-56
[root@HE3 lib64]# cd /usr/lib64/
[root@HE3 ~]# ln -s libperconaserverclient_r.so.18 libperconaserverclient_r.so
Warning:警告1
这里该命令一直过不去yum install --enablerepo=Percona56 Percona-Server-shared-56,后直接安装的Percona-Server-shared-56通过
Warning:警告2
跟据glib安装的路径,修改SQLAdvisor/sqladvisor/CMakeLists.txt中的两处include_directories针对glib设置的path。本文采用yum安装的git,故glib yum 安装默认不需要修改路径
Warning:警告3
编译sqladvisor时依赖perconaserverclient_r, 因此需要安装Percona-Server-shared-56。有可能需要配置软链接例如:1. cd /usr/lib64/ 2. ln -s libperconaserverclient_r.so.18 libperconaserverclient_r.so
Warning:警告4
有可能需要配置percona56 yum源: yum install http://www.percona.com/downloads/percona-release/redhat/0.1-3/percona-release-0.1-3.noarch.rpm
[root@HE3 ~]#cmake -DBUILD_CONFIG=mysql_release -DCMAKE_BUILD_TYPE=debug -DCMAKE_INSTALL_PREFIX=/usr/local/sqlparser ./
[root@HE3 ~]# make && make install
[root@HE3 ~]# cd SQLAdvisor/sqladvisor
[root@HE3 sqladvisor]# cmake -DCMAKE_BUILD_TYPE=debug ./
[root@HE3 sqladvisor]# make
在本路径下生成一个sqladvisor可执行文件,这即是我们想要的。
使用
Part1:对小表进行测试
[root@HE3 sqladvisor]# ./sqladvisor -h 127.0.0.1 -P 3306 -u root -p "MANAGER" -d helei1 -q "select * from helei1" -v 1
2017-03-21 20:37:53 8581 [Note] 第1步: 对SQL解析优化之后得到的SQL:select `*` AS `*` from `helei1`.`helei1`
2017-03-21 20:37:53 8581 [Note] 第2步:表helei1 的SQL太逆天,没有优化建议
2017-03-21 20:37:53 8581 [Note] 第3步: SQLAdvisor结束!
Part2:对大表进行测试(有索引)
这里我们对表helei进行分析,改表在c1列上存在索引
[root@HE3 sqladvisor]# ./sqladvisor -h 127.0.0.1 -P 3306 -u root -p "MANAGER" -d helei1 -q "explain select * from helei where c1=88501;" -v 1
2017-03-21 21:19:23 8624 [Note] 第1步: 对SQL解析优化之后得到的SQL:select `*` AS `*` from `helei1`.`helei` where (`c1` = 88501)
2017-03-21 21:19:23 8624 [Note] 第2步:开始解析where中的条件:(`c1` = 88501)
2017-03-21 21:19:23 8624 [Note] show index from helei
2017-03-21 21:19:23 8624 [Note] show table status like 'helei'
2017-03-21 21:19:23 8624 [Note] select count(*) from ( select `c1` from `helei` FORCE INDEX( PRIMARY ) order by id DESC limit 10000) `helei` where (`c1` = 88501)
2017-03-21 21:19:23 8624 [Note] 第3步:表helei的行数:200380,limit行数:10000,得到where条件中(`c1` = 88501)的选择度:10000
2017-03-21 21:19:23 8624 [Note] 第4步:开始验证 字段c1是不是主键。表名:helei
2017-03-21 21:19:23 8624 [Note] show index from helei where Key_name = 'PRIMARY' and Column_name ='c1' and Seq_in_index = 1
2017-03-21 21:19:23 8624 [Note] 第5步:字段c1不是主键。表名:helei
2017-03-21 21:19:23 8624 [Note] 第6步:开始验证 字段c1是不是主键。表名:helei
2017-03-21 21:19:23 8624 [Note] show index from helei where Key_name = 'PRIMARY' and Column_name ='c1' and Seq_in_index = 1
2017-03-21 21:19:23 8624 [Note] 第7步:字段c1不是主键。表名:helei
2017-03-21 21:19:23 8624 [Note] 第8步:开始验证表中是否已存在相关索引。表名:helei, 字段名:c1, 在索引中的位置:1
2017-03-21 21:19:23 8624 [Note] show index from helei where Column_name ='c1' and Seq_in_index =1
2017-03-21 21:19:23 8624 [Note] 第9步:索引(c1)已存在
2017-03-21 21:19:23 8624 [Note] 第10步: SQLAdvisor结束!
可以看到,最后给出了该条SQL已经拥有有效索引的建议
Part2:对大表进行测试(无索引)
这里我们对表helei进行分析,改表在c5列上没有索引
[root@HE3 sqladvisor]# ./sqladvisor -h 127.0.0.1 -P 3306 -u root -p "MANAGER" -d helei1 -q "explain select * from helei where c5=74685;" -v 1
2017-03-21 21:20:53 8628 [Note] 第1步: 对SQL解析优化之后得到的SQL:select `*` AS `*` from `helei1`.`helei` where (`c5` = 74685)
2017-03-21 21:20:53 8628 [Note] 第2步:开始解析where中的条件:(`c5` = 74685)
2017-03-21 21:20:53 8628 [Note] show index from helei
2017-03-21 21:20:53 8628 [Note] show table status like 'helei'
2017-03-21 21:20:53 8628 [Note] select count(*) from ( select `c5` from `helei` FORCE INDEX( PRIMARY ) order by id DESC limit 10000) `helei` where (`c5` = 74685)
2017-03-21 21:20:53 8628 [Note] 第3步:表helei的行数:201361,limit行数:10000,得到where条件中(`c5` = 74685)的选择度:10000
2017-03-21 21:20:53 8628 [Note] 第4步:开始验证 字段c5是不是主键。表名:helei
2017-03-21 21:20:53 8628 [Note] show index from helei where Key_name = 'PRIMARY' and Column_name ='c5' and Seq_in_index = 1
2017-03-21 21:20:53 8628 [Note] 第5步:字段c5不是主键。表名:helei
2017-03-21 21:20:53 8628 [Note] 第6步:开始验证 字段c5是不是主键。表名:helei
2017-03-21 21:20:53 8628 [Note] show index from helei where Key_name = 'PRIMARY' and Column_name ='c5' and Seq_in_index = 1
2017-03-21 21:20:53 8628 [Note] 第7步:字段c5不是主键。表名:helei
2017-03-21 21:20:53 8628 [Note] 第8步:开始验证表中是否已存在相关索引。表名:helei, 字段名:c5, 在索引中的位置:1
2017-03-21 21:20:53 8628 [Note] show index from helei where Column_name ='c5' and Seq_in_index =1
2017-03-21 21:20:53 8628 [Note] 第9步:开始输出表helei索引优化建议:
2017-03-21 21:20:53 8628 [Note] Create_Index_SQL:alter table helei add index idx_c5(c5)
2017-03-21 21:20:53 8628 [Note] 第10步: SQLAdvisor结束!
可以看到,最后给出了创建索引的建议
Part3:多条SQL同时分析
可以创建任意名的参数文件,这里叫helei.cnf,输入常规的数据库连接信息和SQL,SQL之间用分号隔开。
[root@HE3 sqladvisor]# cat helei.cnf
[sqladvisor]
username=root
password=MANAGER
host=127.0.0.1
port=3306
dbname=helei1
sqls=select * from helei where c1=88501;select * from helei where c5=74685;
这里使用-f命令来载入helei.cnf中的配置
[root@HE3 sqladvisor]# ./sqladvisor -f helei.cnf -v 1
2017-03-21 21:27:35 8640 [Note] 第1步: 对SQL解析优化之后得到的SQL:select `*` AS `*` from `helei1`.`helei` where (`c1` = 88501)
2017-03-21 21:27:35 8640 [Note] 第2步:开始解析where中的条件:(`c1` = 88501)
2017-03-21 21:27:35 8640 [Note] show index from helei
2017-03-21 21:27:35 8640 [Note] show table status like 'helei'
2017-03-21 21:27:35 8640 [Note] select count(*) from ( select `c1` from `helei` FORCE INDEX( PRIMARY ) order by id DESC limit 10000) `helei` where (`c1` = 88501)
2017-03-21 21:27:35 8640 [Note] 第3步:表helei的行数:200674,limit行数:10000,得到where条件中(`c1` = 88501)的选择度:10000
2017-03-21 21:27:35 8640 [Note] 第4步:开始验证 字段c1是不是主键。表名:helei
2017-03-21 21:27:35 8640 [Note] show index from helei where Key_name = 'PRIMARY' and Column_name ='c1' and Seq_in_index = 1
2017-03-21 21:27:35 8640 [Note] 第5步:字段c1不是主键。表名:helei
2017-03-21 21:27:35 8640 [Note] 第6步:开始验证 字段c1是不是主键。表名:helei
2017-03-21 21:27:35 8640 [Note] show index from helei where Key_name = 'PRIMARY' and Column_name ='c1' and Seq_in_index = 1
2017-03-21 21:27:35 8640 [Note] 第7步:字段c1不是主键。表名:helei
2017-03-21 21:27:35 8640 [Note] 第8步:开始验证表中是否已存在相关索引。表名:helei, 字段名:c1, 在索引中的位置:1
2017-03-21 21:27:35 8640 [Note] show index from helei where Column_name ='c1' and Seq_in_index =1
2017-03-21 21:27:35 8640 [Note] 第9步:索引(c1)已存在
2017-03-21 21:27:35 8640 [Note] 第10步: SQLAdvisor结束!
2017-03-21 21:27:35 8640 [Note] 第1步: 对SQL解析优化之后得到的SQL:select `*` AS `*` from `helei1`.`helei` where (`c5` = 74685)
2017-03-21 21:27:35 8640 [Note] 第2步:开始解析where中的条件:(`c5` = 74685)
2017-03-21 21:27:35 8640 [Note] show index from helei
2017-03-21 21:27:35 8640 [Note] show table status like 'helei'
2017-03-21 21:27:35 8640 [Note] select count(*) from ( select `c5` from `helei` FORCE INDEX( PRIMARY ) order by id DESC limit 10000) `helei` where (`c5` = 74685)
2017-03-21 21:27:35 8640 [Note] 第3步:表helei的行数:201067,limit行数:10000,得到where条件中(`c5` = 74685)的选择度:10000
2017-03-21 21:27:35 8640 [Note] 第4步:开始验证 字段c5是不是主键。表名:helei
2017-03-21 21:27:35 8640 [Note] show index from helei where Key_name = 'PRIMARY' and Column_name ='c5' and Seq_in_index = 1
2017-03-21 21:27:35 8640 [Note] 第5步:字段c5不是主键。表名:helei
2017-03-21 21:27:35 8640 [Note] 第6步:开始验证 字段c5是不是主键。表名:helei
2017-03-21 21:27:35 8640 [Note] show index from helei where Key_name = 'PRIMARY' and Column_name ='c5' and Seq_in_index = 1
2017-03-21 21:27:35 8640 [Note] 第7步:字段c5不是主键。表名:helei
2017-03-21 21:27:35 8640 [Note] 第8步:开始验证表中是否已存在相关索引。表名:helei, 字段名:c5, 在索引中的位置:1
2017-03-21 21:27:35 8640 [Note] show index from helei where Column_name ='c5' and Seq_in_index =1
2017-03-21 21:27:35 8640 [Note] 第9步:开始输出表helei索引优化建议:
2017-03-21 21:27:35 8640 [Note] Create_Index_SQL:alter table helei add index idx_c5(c5)
2017-03-21 21:27:35 8640 [Note] 第10步: SQLAdvisor结束!
可以看到这里对helei.cnf中的两个SQL进行了一次性建议输出。
Warning:警告5
SQL中的子查询、or条件、使用函数的条件 会忽略不处理。
命令行传入sql参数时,注意sql中的双引号、反引号 都需要用\转义。建议使用配置文件形式调用
——总结——
可以看出,SQLAdvisor对查询SQL给出了索引创建的优化和建议,还是不错的,后续会进一步进行深度测试。由于笔者的水平有限,编写时间也很仓促,文中难免会出现一些错误或者不准确的地方,不妥之处恳请读者批评指正。
参考:https://github.com/Meituan-Dianping/SQLAdvisor/blob/master/doc/QUICK_START.md
SQLAdvisor美团SQL索引优化建议工具相关推荐
- mysql advisor github_GitHub - zyw/sqladvisor-web: 美团SQLAdvisor SQL优化建议工具的Web版,告别命令行...
sqladvisor-web 美团SQLAdvisor SQL优化建议工具的Web版,告别命令行. 项目中使用的美团SQL分析工具是在CentOS上编译的,所以建议部署到CentOS上. 该项目是使用 ...
- db2advis DB2索引优化建议
db2advis DB2索引优化建议 用途:db2advis用于自动根据表.索引.物化视图统计值,自动分析sql执行效率,并给出优化建议: 此命令可以在服务器上执行,也可以在客户端执行(需要先进行编目 ...
- MySQL数据库性能优化由浅入深(表设计、慢查询、SQL索引优化、Explain分析、Show Profile分析、配置优化)
文章目录 0 SQL性能分析 1 表的设计合理化 1.1 为什么需要范式 1.2 三范式原理 1.3 什么样的表才满足三范式 2 慢查询 2.1 慢查询介绍 2.2 慢查询步骤 3 添加适当索引 3. ...
- SQL索引优化之explain查询计划
前言 最近工作当中使用SQL的场景比较多,遇到了一些瓶颈,在SQL优化这方面做了一些了解. 在SQL应用中影响性能最多的就是慢查询,关于慢查询的优化,主要有这几个着手的方向: 监控sql执行情况,发邮 ...
- sql索引优化之日期:between与大于小于
而无论是否在同一天,用>=和<=时,与between相似. 查询的时间是加了默认的betree索引的.在between同一天的时候,索引有效: 当between不在同一天时,索引无效: 而 ...
- 码农必备SQL高性能优化指南。35+条优化建议立马get
由于工作需要,最近做了很多 BI 取数的工作,需要用到一些比较高级的 SQL 技巧,总结了一下工作中用到的一些比较骚的进阶技巧,特此记录一下,以方便自己查阅,主要目录如下: SQL 的书写规范 SQL ...
- 慢 SQL 诊断优化套路包,套路用的对,速度升百倍
在DBA的日常工作中,调整个别性能较差的SQL语句是一项富有挑战性的工作.面对慢SQL,一些DBA会心烦,会沮丧,会束手无措,也会沉着冷静.斗智斗勇!当中的关键在于数据库管理员如何分析得出SQL语句的 ...
- 数据库 | SQL 诊断优化套路包,套路用的对,速度升百倍
本文出自头条号老王谈运维,转载请说明出处. 前言 在DBA的日常工作中,调整个别性能较差的SQL语句是一项富有挑战性的工作.面对慢SQL,一些DBA会心烦,会沮丧,会束手无措,也会沉着冷静.斗智斗勇! ...
- 数据库优化:SQL高性能优化指南,助你成就大神之路!
1.参数是子查询时,使用 EXISTS 代替 IN 如果 IN 的参数是(1,2,3)这样的值列表时,没啥问题,但如果参数是子查询时,就需要注意了.比如,现在有如下两个表: 现在我们要查出同时存在于两 ...
最新文章
- poj 1324(BFS+状态压缩)
- 12.04 深圳站 | Serverless Developer Meetup 开放报名
- node11---相册
- 中绘制折线_漂亮图表也可信手拈来,一文学会用Python绘制堆积折线图
- JDK Dynamic Proxy_JDK动态代理
- [转]VS2008 集成openCV过程
- Understand层级关系视图中几种分类的介绍
- php @file_exists 前面加at是什么意思,PHP file_exists()函数前加反斜杠“\”是什么意思?...
- 长期大量收售通信 联通 移动 电信 工程各种型号光缆
- Shiro 之 Subject 类
- 上海最新住房贷款(含公积金贷款,商业贷款,组合贷款)
- canvas画一个渐变色的环形进度环
- c# 去掉字符串最后一个逗号
- 云游昆大丽(八)——木府流芳
- 低代码编程核心技术概念
- 山外看山 —— 评The Art of Unix Programming
- 玛雅预言2012的误区
- 傅里叶变换尺度变换性质_图像处理之傅里叶变换
- A-Level Economics 真题及答案解析
- Linux九阴真经之九阴白骨爪残卷4(cobbler)