美文网首页
soar-php 2.0 - SQL 语句优化器和重写器的 PH

soar-php 2.0 - SQL 语句优化器和重写器的 PH

作者: guanguans | 来源:发表于2020-03-17 11:44 被阅读0次

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

image

环境要求

  • 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>
...

参考链接

License

MIT

相关文章

  • soar-php 2.0 - SQL 语句优化器和重写器的 PH

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

  • soar-PHP - SQL 语句优化器和重写器的 PHP 扩展

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

  • PostgreSQL Executor(0): Overview

    用户输入的SQL语句经过解析、分析、查询重写,而后在优化器中生成优化后的执行计划。这一过程可以称为查询编译,它的目...

  • MySql性能调优三(explain/desc执行计划)

    前言 explian/desc可以帮助我们分析sql语句,写出高效sql语句,让mysql查询优化器可以更好的工作...

  • 7 Explain详解

    使用Explain关键字可以模拟优化器执行SQL查询语句,从而指导MySQL是如何处理SQL语句的.分析查询语句或...

  • MySQL查询优化(分析、索引、配置等)

    数据库的优化包括两个方面,一是SQL语句的优化,二是数据库服务器和配置的优化。下面先讲查询语句的优化。 查询语句优...

  • mysql explain详解

    使用EXPLAIN关键字可以模拟优化器执行SQL语句,从而知道MySQL是如何处理你的SQL语句的。可以分析SQL...

  • 深入理解MySql的Explain

    explain关键字可以模拟优化器执行SQL语句,从而知道MySQL是 如何处理你的SQL语句的。分析你的查询语句...

  • MySQL优化

    MySQL架构 网络连接层 服务层连接池系统管理和控制SQL接口解析器, 对SQL语句检查,生成解析树查询优化器缓...

  • 2022-10-14

    explain关键字可以模拟MySQL优化器执行SQL语句,可以很好的分析SQL语句或表结构的性能瓶颈。 expl...

网友评论

      本文标题:soar-php 2.0 - SQL 语句优化器和重写器的 PH

      本文链接:https://www.haomeiwen.com/subject/adazehtx.html