基于mysql的SQLadvisor工具一次优化尝试
使用方式:
1 1> [root@SQLAdvisor ~]# getenforce 2 Disabled 3 安装SQLAdvisor 4 [root@SQLAdvisor ~]# yum -y install cmake libaio-devel libffi-devel glib2 glib2-devel
2> 配置Percona56 yum源;
1 $ yum install http://www.percona.com/downloads/percona-release/redhat/0.1-3/percona-release-0.1-3.noarch.rpm
# 安装Percona-Server-shared-56;
1 $ yum install Percona-Server-shared-56
3> 查看版本,测试是否安装成功。
1 [root@xjfw3 ~]# cmake --version 2 cmake version 3.10.0-rc4 3 CMake suite maintained and supported by Kitware (kitware.com/cmake).
4> 建立软连接
1 [root@SQLAdvisor ~]# ln -s /usr/lib64/libperconaserverclient_r.so.18 /usr/lib64/libperconaserverclient_r.so
5> 编译依赖项sqlparser
1 [root@SQLAdvisor ~]# cd SQLAdvisor/ 2 [root@SQLAdvisor SQLAdvisor]# cmake -DBUILD_CONFIG=mysql_release -DCMAKE_BUILD_TYPE=debug -DCMAKE_INSTALL_PREFIX=/usr/local/sqlparser ./ 3 [root@SQLAdvisor SQLAdvisor]# make && make install
6> 安装SQLAdvisor
1 [root@SQLAdvisor SQLAdvisor]# cd sqladvisor/ 2 [root@SQLAdvisor sqladvisor]# cmake -DCMAKE_BUILD_TYPE=debug ./ 3 [root@SQLAdvisor sqladvisor]# make 4 [root@SQLAdvisor sqladvisor]# ./sqladvisor --help
#####################################自己环境下的一次测试#############################
1 mysql> create database test1 character set utf8mb4; 2 Query OK, 1 row affected (0.00 sec) 3 4 mysql> create table user( 5 -> id INT PRIMARY KEY AUTO_INCREMENT, 6 -> name VARCHAR(64) NOT NULL, 7 -> age int, 8 -> sex int 9 -> )ENGINE=INNODB DEFAULT CHARSET=utf8mb4; 10 Query OK, 0 rows affected (0.13 sec) 11 12 mysql> desc user; 13 +-------+-------------+------+-----+---------+----------------+ 14 | Field | Type | Null | Key | Default | Extra | 15 +-------+-------------+------+-----+---------+----------------+ 16 | id | int(11) | NO | PRI | NULL | auto_increment | 17 | name | varchar(64) | NO | | NULL | | 18 | age | int(11) | YES | | NULL | | 19 | sex | int(11) | YES | | NULL | | 20 +-------+-------------+------+-----+---------+----------------+ 21 4 rows in set (0.01 sec) 22 23 * 生成测试数据 24 mysql> insert into user(name,age, sex) select 'lisea', 25, 1; 25 Query OK, 1 row affected (0.01 sec) 26 Records: 1 Duplicates: 0 Warnings: 0 27 生产测试数据 28 insert into user(name,age, sex) select concat(name, '1'), age+1, sex+1 from user; 29 insert into user(name,age, sex) select concat(name, '2'), age+2, sex from user; 30 insert into user(name,age, sex) select concat(name, '3'), age+2, sex from user; 31 insert into user(name,age, sex) select concat(name, '10'), age+2, sex from user; 32 insert into user(name,age, sex) select concat(name, '11'), age+4, sex from user; 33 --执行两次 34 35 结果 36 [mysql@mysql sqladvisor]$ ./sqladvisor -h 192.168.226.131 -P 16063 -u root -p '6yhn^YHN' -d test1 -q "select * from user where name = 'lisea'" -v 1 37 2019-10-24 07:36:20 35965 [Note] 第1步: 对SQL解析优化之后得到的SQL:select `*` AS `*` from `test1`.`user` where (`name` = 'lisea') 38 2019-10-24 07:36:20 35965 [Note] 第2步:开始解析where中的条件:(`name` = 'lisea') 39 2019-10-24 07:36:20 35965 [Note] show index from user 40 2019-10-24 07:36:20 35965 [Note] show table status like 'user' 41 2019-10-24 07:36:20 35965 [Note] select count(*) from ( select `name` from `user` FORCE INDEX( PRIMARY ) order by id DESC limit 10000) `user` where (`name` = 'lisea') 42 2019-10-24 07:36:20 35965 [Note] 第3步:表user的行数:1045485,limit行数:10000,得到where条件中(`name` = 'lisea')的选择度:10000 43 2019-10-24 07:36:20 35965 [Note] 第4步:开始验证 字段name是不是主键。表名:user 44 2019-10-24 07:36:20 35965 [Note] show index from user where Key_name = 'PRIMARY' and Column_name ='name' and Seq_in_index = 1 45 2019-10-24 07:36:20 35965 [Note] 第5步:字段name不是主键。表名:user 46 2019-10-24 07:36:20 35965 [Note] 第6步:开始验证 字段name是不是主键。表名:user 47 2019-10-24 07:36:20 35965 [Note] show index from user where Key_name = 'PRIMARY' and Column_name ='name' and Seq_in_index = 1 48 2019-10-24 07:36:20 35965 [Note] 第7步:字段name不是主键。表名:user 49 2019-10-24 07:36:20 35965 [Note] 第8步:开始验证表中是否已存在相关索引。表名:user, 字段名:name, 在索引中的位置:1 50 2019-10-24 07:36:20 35965 [Note] show index from user where Column_name ='name' and Seq_in_index =1 51 2019-10-24 07:36:20 35965 [Note] 第9步:开始输出表user索引优化建议: 52 2019-10-24 07:36:20 35965 [Note] Create_Index_SQL:alter table user add index idx_name(name) 53 2019-10-24 07:36:20 35965 [Note] 第10步: SQLAdvisor结束! 54 55 也可配置文件传参调用 56 [root@SQLAdvisor sqladvisor]# cat sql.cnf 57 [sqladvisor] 58 username=root 59 password=123 60 host=127.0.0.1 61 port=3306 62 dbname=test1 63 sqls=select * from user where name = 'lisea' 64 [root@SQLAdvisor sqladvisor]# ./sqladvisor -f sql.cnf -v 1