Rootop 服务器运维与web架构

使用SQLAdvisor对SQL语句分析索引优化建议

SQLAdvisor是美团做的一个分析sql给出索引优化建议的工具。

建议放到一个独立的系统或者docker中。

系统:centos7
项目地址:https://github.com/Meituan-Dianping/SQLAdvisor

安装步骤:
# 拉取代码

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

# 安装percona yum源

[root@localhost ~]# wget -c https://www.percona.com/redir/downloads/percona-release/redhat/latest/percona-release-0.1-4.noarch.rpm
[root@localhost ~]# rpm -ivh percona-release-0.1-4.noarch.rpm

# 安装依赖

[root@localhost ~]# yum install -y Percona-Server-shared-56 cmake libaio-devel libffi-devel glib2 glib2-devel openssl-devel

# 编译依赖项sqlparser

[root@localhost ~]# cd /root/SQLAdvisor
[root@localhost SQLAdvisor]# cmake -DBUILD_CONFIG=mysql_release -DCMAKE_BUILD_TYPE=debug -DCMAKE_INSTALL_PREFIX=/usr/local/sqlparser ./
[root@localhost SQLAdvisor]# make && make install

# 安装SQLAdvisor

[root@localhost ~]# cd /root/SQLAdvisor/sqladvisor/
[root@localhost sqladvisor]# cmake -DCMAKE_BUILD_TYPE=debug ./
[root@localhost sqladvisor]# make
然后在当前路径下生成一个 sqladvisor 的可执行文件。

# 拷贝到/usr/local/bin/下便于命令调用

[root@localhost sqladvisor]# cp sqladvisor /usr/local/bin/

# 查看sqladvisor支持的参数

[root@localhost ~]# sqladvisor --help
-f, --defaults-file sqls file
-u, --username username
-p, --password password
-P, --port port
-h, --host host
-d, --dbname database name
-q, --sqls sqls
-v, --verbose 1:output logs 0:output nothing

# 命令行传参调用

[root@localhost ~]# sqladvisor -h x -P x -u x -p 'x' -d x -q "sql" -v 1
注意命令行传参时,参数名与值需要用空格隔开

# 使用配置文件传参调用

[root@localhost ~]# cat sql.cnf
[sqladvisor]
username=root
password=root
host=192.168.10.24
port=3306
dbname=test
sqls=SELECT * FROM sys_login_log WHERE userId = 100580;

如果多个sql语句格式即 sql=sql1;sql2;sql3…
# 开始分析

[root@localhost ~]# sqladvisor -f sql.cnf -v 1
第1步: 对SQL解析优化之后得到的SQL:select `*` AS `*` from `test`.`sys_login_log` where (`userId` = 100580)
第2步:开始解析where中的条件:(`userId` = 100580)
show index from sys_login_log
show table status like 'sys_login_log'
select count(*) from ( select `userId` from `sys_login_log` FORCE INDEX( PRIMARY ) order by id DESC limit 10000) `sys_login_log` where (`userId` = 100580)
第3步:表sys_login_log的行数:365166,limit行数:10000,得到where条件中(`userId` = 100580)的选择度:10000
第4步:开始验证 字段userId是不是主键。表名:sys_login_log
show index from sys_login_log where Key_name = 'PRIMARY' and Column_name ='userId' and Seq_in_index = 1
第5步:字段userId不是主键。表名:sys_login_log
第6步:开始验证 字段userId是不是主键。表名:sys_login_log
show index from sys_login_log where Key_name = 'PRIMARY' and Column_name ='userId' and Seq_in_index = 1
第7步:字段userId不是主键。表名:sys_login_log
第8步:开始验证表中是否已存在相关索引。表名:sys_login_log, 字段名:userId, 在索引中的位置:1
show index from sys_login_log where Column_name ='userId' and Seq_in_index =1
第9步:开始输出表sys_login_log索引优化建议:
Create_Index_SQL:alter table sys_login_log add index idx_userId(userId)
第10步: SQLAdvisor结束!

提示 创建索引 Create_Index_SQL:alter table sys_login_log add index idx_userId(userId)


 

原创文章,转载请注明。本文链接地址: https://www.rootop.org/pages/4036.html

作者:Venus

服务器运维与性能优化

评论已关闭。