soar-PHP 2.0 - SQL 语句优化器和重写器的 PHP 扩展包、 方便 SQL 语句调优

soar-php 是一个基于小米公司开源的 soar 开发的 PHP 扩展包,方便框架中 SQL 语句调优。

环境要求

  • PHP >= 7.1
  • ext-pdo

框架中使用

安装

$ composer require guanguans/soar-php --dev

使用

下载 XiaoMi 开源的 SQL 优化器 soar,更多详细安装请参考 soar install

# macOS
$ wget https://github.com/XiaoMi/soar/releases/download/0.11.0/soar.darwin-amd64
# linux
$ wget https://github.com/XiaoMi/soar/releases/download/0.11.0/soar.linux-amd64
# windows
$ wget https://github.com/XiaoMi/soar/releases/download/0.11.0/soar.windows-amd64
# 用其他命令或下载器下载均可以

初始化配置,更多详细配置请参考 soar config

方法一、运行时初始化配置

<?php

require_once __DIR__.'/vendor/autoload.php';

use Guanguans\SoarPHP\Soar;

$config = [
    // 下载的 soar 的路径
    '-soar-path' => '/Users/yaozm/Documents/wwwroot/soar-php/soar.darwin-amd64',
    // 测试环境配置
    '-test-dsn' => [
        'host' => '127.0.0.1',
        'port' => '3306',
        'dbname' => 'database',
        'username' => 'root',
        'password' => '123456',
    ],
    // 日志输出文件
    '-log-output' => './soar.log',
    // 报告输出格式: 默认  markdown [markdown, html, json]
    '-report-type' => 'html',
];
$soar = new Soar($config);

方法二、配置文件初始化配置

vendor 同级目录下新建 .soar.dist 或者 .soar,内容参考 .soar.example,例如:

<?php
return [
    // 下载的 soar 的路径
    '-soar-path' => '/Users/yaozm/Documents/wwwroot/soar-php/soar.darwin-amd64',
    // 测试环境配置
    '-test-dsn' => [
        'host' => '127.0.0.1',
        'port' => '3306',
        'dbname' => 'database',
        'username' => 'root',
        'password' => '123456',
    ],
    // 日志输出文件
    '-log-output' => './soar.log',
    // 报告输出格式: 默认  markdown [markdown, html, json]
    '-report-type' => 'html',
];

然后初始化

<?php

require_once __DIR__.'/vendor/autoload.php';

use Guanguans\SoarPHP\Soar;

$soar = new Soar();

配置优先级:运行时初始化配置 > .soar > .soar.dist

SQL 评分

方法调用:

$sql ="SELECT * FROM `fa_user` `user` LEFT JOIN `fa_user_group` `group` ON `user`.`group_id`=`group`.`id`;";
echo $soar->score($sql);

输出结果:

score.png

explain 信息解读

方法调用:

$sql = "SELECT * FROM `fa_auth_group_access` `aga` LEFT JOIN `fa_auth_group` `ag` ON `aga`.`group_id`=`ag`.`id`;";
// 输出 html 格式
echo $soar->htmlExplain($sql);
// 输出 md 格式
echo $soar->mdExplain($sql);
// 输出 html 格式
echo $soar->explain($sql, 'html');
// 输出 md 格式
echo $soar->explain($sql, 'md');

输出结果:

explain.png

语法检查

方法调用:

$sql = 'selec * from fa_user';
echo $soar->syntaxCheck($sql);

输出结果:

At SQL 1 : line 1 column 5 near "selec * from fa_user" (total length 20)

SQL 指纹

方法调用:

$sql = 'select * from fa_user where id=1';
echo $soar->fingerPrint($sql);

输出结果:

select * from fa_user where id = ?

SQL 美化

方法调用:

$sql = 'select * from fa_user where id=1';
var_dump($soar->pretty($sql));

输出结果:

SELECT  
  * 
FROM  
  fa_user  
WHERE  
  id  = 1;

markdown 转化为 html

方法调用:

echo $soar->md2html("## 这是一个测试");

输出结果:

...
<h2>这是一个测试</h2>
...

soar 帮助

方法调用:

var_dump($soar->help());

输出结果:

···
'Usage of /Users/yaozm/Documents/wwwroot/soar-php/soar:
  -allow-charsets string
        AllowCharsets (default "utf8,utf8mb4")
  -allow-collates string
        AllowCollates
  -allow-drop-index
        AllowDropIndex, 允许输出删除重复索引的建议
  -allow-engines string
        AllowEngines (default "innodb")
  -allow-online-as-test
        AllowOnlineAsTest, 允许线上环境也可以当作测试环境
  -blacklist string
        指定 blacklist 配置文件的位置,文件中的 SQL 不会被评审。
···    

执行任意 soar 命令

方法调用:

$command = "echo '## 这是另一个测试' | /Users/yaozm/Documents/wwwroot/soar-php/soar.darwin-amd64 -report-type md2html";
echo $soar->exec($command);

输出结果:

...
<h2>这是另一个测试</h2>
...

贡献者 ✨

kamly
kamly

🐛
Leslie Lau
Leslie Lau

🐛
D.J.Hwang
D.J.Hwang

🤔

参考链接

License

MIT

本作品采用《CC 协议》,转载必须注明作者和本文链接
No practice, no gain in one's wit. 我的 Gitub
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!
未填写
文章
59
粉丝
133
喜欢
994
收藏
1352
排名:45
访问:15.5 万
私信
所有博文
社区赞助商