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 优化建议
{
"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"
]
},
...
]
}
实现该接口
<?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\JsonOutput::class,
\Guanguans\LaravelSoar\Outputs\LogOutput::class => ['channel' => 'daily'],
\Guanguans\LaravelSoar\Outputs\DebugBarOutput::class,
\Guanguans\LaravelSoar\Outputs\SoarBarOutput::class,
],
...
];
Soar 实例及方法
soar();
app('soar');
class Soar{}
查询构建器方法
namespace Illuminate\Database\Eloquent {
class Builder
{
}
}
|