关于php:laravelsoar2x-自动监控输出-SQL-优化建议辅助-laravel-应用-SQL-优化

3次阅读

共计 3762 个字符,预计需要花费 10 分钟才能阅读完成。

laravel-soar – 主动监控输入 SQL 优化倡议、辅助 laravel 利用 SQL 优化。

源码

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

性能

  • 反对启发式算法语句优化倡议、索引优化倡议
  • 反对 EXPLAIN 信息丰盛解读
  • 主动监控输入 SQL 优化倡议
  • Debug bar、Soar bar、JSON、Clockwork、Console、Dump、Log、自定义输入器 (多种场景输入)
  • 反对查问构建器生成 SQL 优化倡议

装置

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

配置

注册服务

laravel

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

lumen

将以下代码段增加到 bootstrap/app.php 文件中的 Register Service Providers 局部下:

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

应用 (示例代码)

主动监控输入 SQL 优化倡议

  • Json 响应 (残缺示例)
{
    "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

  • 自定义输入器

实现该接口

<?php

namespace Guanguans\LaravelSoar\Contracts;

use Illuminate\Support\Collection;

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

config/soar.php 文件中配置输入器即可

<?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 实例及办法

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{}

查问构建器办法

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
    {}}
正文完
 0