2

laravel-soar - Automatically monitor and output SQL optimization suggestions, assist laravel to apply SQL optimization.

source code

https://github.com/guanguans/laravel-soar

Features

  • Support heuristic algorithm statement optimization suggestions, index optimization suggestions
  • Support EXPLAIN rich information interpretation
  • Automatically monitor output SQL optimization recommendations
  • Debug bar, Soar bar, JSON, Clockwork, Console, Dump, Log, custom output device (multiple scene output)
  • Support query builder to generate SQL optimization suggestions

Install

 $ composer require guanguans/laravel-soar --dev -vvv

configure

registration service

laravel

 $ php artisan vendor:publish --provider="Guanguans\\LaravelSoar\\SoarServiceProvider"

lumen

Add the following snippet to the bootstrap/app.php file under the Register Service Providers section:

 $app->register(\Guanguans\LaravelSoar\SoarServiceProvider::class);

use ( sample code )

Automatically monitor output SQL optimization recommendations

 {
    "message": "ok",
    "soar_scores": [
        {
            "Summary": "[☆☆☆☆☆|0分|3.56ms|select * from `users` where `name` = 'soar' group by `name` having `created_at` > '2022-04-19 18:24:33']",
            "HeuristicRules": [
               ...
                {
                    "Item": "GRP.001",
                    "Severity": "L2",
                    "Summary": "不建议对等值查询列使用 GROUP BY",
                    "Content": "GROUP BY 中的列在前面的 WHERE 条件中使用了等值查询,对这样的列进行 GROUP BY 意义不大。",
                    "Case": "select film_id, title from film where release_year='2006' group by release_year",
                    "Position": 0
                },
               ...
            ],
            "IndexRules": [
                {
                    "Item": "IDX.001",
                    "Severity": "L2",
                    "Summary": "为laravel库的users表添加索引",
                    "Content": "为列name添加索引;为列created_at添加索引; 由于未开启数据采样,各列在索引中的顺序需要自行调整。",
                    "Case": "ALTER TABLE `laravel`.`users` add index `idx_name_created_at` (`name`(191),`created_at`) ;\n",
                    "Position": 0
                }
            ],
            "Explain": [],
            "Backtraces": [
                "#13 /app/Admin/Controllers/HomeController.php:74",
                "#55 /Users/yaozm/Documents/develop/laravel-soar/src/Http/Middleware/OutputSoarScoreMiddleware.php:45",
                "#76 /public/index.php:55",
                "#77 /server.php:21"
            ]
        },
        ...
    ]
}
  • Soar bar

  • Debug bar

  • Clockwork

  • Console

  • Dump

  • Log

  • custom exporter

implement this interface

 <?php

namespace Guanguans\LaravelSoar\Contracts;

use Illuminate\Support\Collection;

interface Output
{
    public function output(Collection $scores, $dispatcher);
}

config/soar.php The output device can be configured in the file

 <?php

return [
    ...
    'output' => [
        // \Guanguans\LaravelSoar\Outputs\ClockworkOutput::class,
        // \Guanguans\LaravelSoar\Outputs\ConsoleOutput::class,
        // \Guanguans\LaravelSoar\Outputs\DumpOutput::class => ['exit' => false],
        \Guanguans\LaravelSoar\Outputs\JsonOutput::class,
        \Guanguans\LaravelSoar\Outputs\LogOutput::class => ['channel' => 'daily'],
        \Guanguans\LaravelSoar\Outputs\DebugBarOutput::class,
        \Guanguans\LaravelSoar\Outputs\SoarBarOutput::class,
    ],
    ...
];

Soar instance and method

 soar();      // 获取 Soar 实例
app('soar'); // 获取 Soar 实例

/**
 * Soar 门面.
 *
 * @method static string score(string $sql)            // SQL 评分
 * @method static array arrayScore(string $sql)        // SQL 数组格式评分
 * @method static string jsonScore(string $sql)        // SQL json 格式评分
 * @method static string htmlScore(string $sql)        // SQL html 格式评分
 * @method static string mdScore(string $sql)          // SQL markdown 格式评分
 * @method static string explain(string $sql)          // explain 解读信息
 * @method static string mdExplain(string $sql)        // markdown 格式 explain 解读信息
 * @method static string htmlExplain(string $sql)      // html 格式 explain 解读信息
 * @method static null|string syntaxCheck(string $sql) // 语法检查
 * @method static string fingerPrint(string $sql)      // SQL 指纹
 * @method static string pretty(string $sql)           // 格式化 SQL
 * @method static string md2html(string $sql)          // markdown 转 html
 * @method static string help()                        // Soar 帮助
 * @method static null|string exec(string $command)    // 执行任意 Soar 命令
 * @method static string getSoarPath()                 // 获取 Soar 路径
 * @method static array getOptions()                   // 获取 Soar 配置选项
 * @method static Soar setSoarPath(string $soarPath)   // 设置 Soar 路径
 * @method static Soar setOption(string $key, $value)  // 设置 Soar 配置选项
 * @method static Soar setOptions(array $options)      // 批量设置 Soar 配置选项
 *
 * @see \Guanguans\SoarPHP\Soar
 * @see \Guanguans\LaravelSoar\Soar
 */
class Soar{}

query builder method

 namespace Illuminate\Database\Eloquent {
    /**
     * @method string toRawSql()
     * @method void   dumpRawSql()
     * @method void   ddRawSql()
     * @method array  toSoarArrayScore()
     * @method void   dumpSoarArrayScore()
     * @method void   ddSoarArrayScore()
     * @method string toSoarJsonScore()
     * @method void   dumpSoarJsonScore()
     * @method void   ddSoarJsonScore()
     * @method string toSoarHtmlScore()
     * @method void   echoSoarHtmlScore()
     * @method void   exitSoarHtmlScore()
     * @method string toSoarHtmlExplain()
     * @method void   echoSoarHtmlExplain()
     * @method void   exitSoarHtmlExplain()
     *
     * @see \Guanguans\LaravelSoar\Support\Macros\QueryBuilderMacro
     */
    class Builder
    {
    }
}

guanguans
2.4k 声望89 粉丝

No practice, no gain in one's wit.