设为首页 收藏本站
查看: 949|回复: 0

[经验分享] SQLAdvisor美团SQL索引优化建议工具

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2017-3-23 15:19:36 | 显示全部楼层 |阅读模式
SQLAdvisor美团SQL索引优化建议工具
前言

Part1:写在最前

SQLAdvisor是美团开源的一款SQL索引优化建议工具,是由美团点评公司技术工程部DBA团队(北京)开发维护的一个分析SQL给出索引优化建议的工具。它基于MySQL原生态词法解析,结合分析SQL中的where条件、聚合条件、多表Join关系 给出索引优化建议。目前SQLAdvisor在美团点评内部广泛应用,公司内部对SQLAdvisor的开发全面转到github上,开源和内部使用保持一致。本文记录对该工具的初步安装和基本使用。


安装

Part1:构建安装环境

[iyunv@HE3 ~]# yum install git

[iyunv@HE3 ~]# git clone https://github.com/Meituan-Dianping/SQLAdvisor.git

[iyunv@HE3 ~]# yum install cmake libaio-devel libffi-devel glib2 glib2-devel

[iyunv@HE3 ~]# yum install http://www.percona.com/downloads ... se-0.1-3.noarch.rpm

[iyunv@HE3 ~]# yum install Percona-Server-shared-56

[iyunv@HE3 lib64]# cd /usr/lib64/

[iyunv@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 ... se-0.1-3.noarch.rpm


[iyunv@HE3 ~]#cmake -DBUILD_CONFIG=mysql_release -DCMAKE_BUILD_TYPE=debug -DCMAKE_INSTALL_PREFIX=/usr/local/sqlparser ./

[iyunv@HE3 ~]# make && make install

[iyunv@HE3 ~]# cd SQLAdvisor/sqladvisor

[iyunv@HE3 sqladvisor]# cmake -DCMAKE_BUILD_TYPE=debug ./

[iyunv@HE3 sqladvisor]# make

在本路径下生成一个sqladvisor可执行文件,这即是我们想要的。


使用

Part1:对小表进行测试

[iyunv@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列上存在索引

[iyunv@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列上没有索引

[iyunv@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之间用分号隔开。

[iyunv@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中的配置

[iyunv@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给出了索引创建的优化和建议,还是不错的,后续会进一步进行深度测试。由于笔者的水平有限,编写时间也很仓促,文中难免会出现一些错误或者不准确的地方,不妥之处恳请读者批评指正。





运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-354231-1-1.html 上篇帖子: 监控SQL Server数据库异常镜像状态发告警邮件 下篇帖子: 快速配置公网访问内网中的sql server数据库 美团
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表