SQLAdvisor美团SQL索引优化建议工具

教程发布:风哥 教程分类:ITPUX技术网 更新日期:2022-02-12 浏览学习:899

SQLAdvisor美团SQL索引优化建议工具

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

安装
Part1:构建安装环境
[root@HE3 ~]# yum -y install git
[root@HE3 ~]# git clone https://github.com/Meituan-Dianping/SQLAdvisor.git
[root@HE3 ~]# yum -y install cmake libaio-devel libffi-devel glib2 glib2-devel
[root@HE3 ~]# yum -y install http://www.percona.com/downloads/percona-release/redhat/0.1-3/percona-release-0.1-3.noarch.rpm
[root@HE3 ~]# yum -y install Percona-Server-shared-56
[root@HE3 lib64]# cd /usr/lib64/
[root@HE3 ~]# ln -sv libperconaserverclient_r.so.18 libperconaserverclient_r.so

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

本文标签:
网站声明:本文由风哥整理发布,转载请保留此段声明,本站所有内容将不对其使用后果做任何承诺,请读者谨慎使用!
【上一篇】
【下一篇】