XiaoMi/soar优化工具-命令版本

小米-DBA开源Soar

https://github.com/XiaoMi/soar

soar加载数据库配置文件

默认文件会按照/etc/soar.yaml, ./etc/soar.yaml, ./soar.yaml顺序加载,找到第一个后不再继续加载后面的配置文件

常用命令

基本用法

echo "select title from sakila.film" | ./soar -log-output=soar.log

指定输入源

# 从文件读取SQL
./soar -query file.sql

# 从管道读取SQL
cat file.sql | ./soar

指定配置文件

vi soar.yaml
# yaml format config file
online-dsn:
    addr:     127.0.0.1:3306
    schema:   sakila
    user:     root
    password: "1t'sB1g3rt"
    disable:  false

test-dsn:
    addr:     127.0.0.1:3306
    schema:   sakila
    user:     root
    password: "1t'sB1g3rt"
    disable:  false
echo "select title from sakila.film" | ./soar -test-dsn="root:1t'[email protected]:3306/sakila" -allow-online-as-test -log-output=soar.log

打印所有的启发式规则

./soar -list-heuristic-rules

语法检查工具

echo "select * from tb" | soar -only-syntax-check
echo $?
0

echo "select * frm tb" | soar -only-syntax-check
At SQL 1 : syntax error at position 13 near 'frm'
echo $?
1

慢日志进行分析示例

pt-query-digest slow.log > slow.log.digest
# parse pt-query-digest's output which example script
python2.7 doc/example/digest_pt.py slow.log.digest > slow.md

SQL指纹

echo "select * from film where col='abc'" | soar -report-type=fingerprint

输出

select * from film where col=?

将 UPDATE/DELETE/INSERT 语法转为 SELECT

echo "update film set title = 'abc'" | soar -rewrite-rules dml2select,delimiter  -report-type rewrite

输出

select * from film;

合并多条ALTER语句

echo "alter table tb add column a int; alter table tb add column b int;" | soar -report-type rewrite -rewrite-rules mergealter

输出

ALTER TABLE `tb` add column a int, add column b int ;

SQL美化

echo "select * from tbl where col = 'val'" | ./soar -report-type=pretty

输出

SELECT
  *
FROM
  tbl
WHERE
  col  = 'val';

EXPLAIN信息分析报告

soar -report-type explain-digest << EOF
+----+-------------+-------+------+---------------+------+---------+------+------+-------+
| id | select_type | table | type | possible_keys | key  | key_len | ref  | rows | Extra |
+----+-------------+-------+------+---------------+------+---------+------+------+-------+
|  1 | SIMPLE      | film  | ALL  | NULL          | NULL | NULL    | NULL | 1131 |       |
+----+-------------+-------+------+---------------+------+---------+------+------+-------+
EOF
##  Explain信息

| id | select\_type | table | partitions | type | possible_keys | key | key\_len | ref | rows | filtered | scalability | Extra |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1  | SIMPLE | *film* | NULL | ALL | NULL | NULL | NULL | NULL | 0 | 0.00% | ☠️ **O(n)** |  |


### Explain信息解读

#### SelectType信息解读

* **SIMPLE**: 简单SELECT(不使用UNION或子查询等).

#### Type信息解读

* ☠️ **ALL**: 最坏的情况, 从头到尾全表扫描.

markdown 转 HTML

通过指定-report-css, -report-javascript, -markdown-extensions, -markdown-html-flags这些参数,你还可以控制HTML的显示格式。

cat test.md | soar -report-type md2html > test.html

清理测试环境残余的临时库表

如配置了-drop-test-temporary=falsesoar异常中止,-test-dsn中会残余以optimizer_为前缀的临时库表。手工清理这些库表可以使用如下命令。

注意:为了不影响正在进行的其他SQL评审,-cleanup-test-database中会删除1小时前生成的临时库表。

./soar -cleanup-test-database

SQL评分规则

不同类型的建议指定的Severity不同,严重程度数字由低到高依次排序。满分100分,扣到0分为止。
L0不扣分只给出建议,L1扣5分,L2扣10分,每级多扣5分以此类推。当由时给出L1, L2两要建议时
扣分叠加,即扣15分。

注意:目前只有markdown和html两种-report-type支持评分输出显示,其他输出格式如有评分需求
可以按上述规则自行计算。

你可能感兴趣的:(XiaoMi/soar优化工具-命令版本)