当前位置:网站首页>laravel-soar(2.x) - 自动监控输出 SQL 优化建议、辅助 laravel 应用 SQL 优化
laravel-soar(2.x) - 自动监控输出 SQL 优化建议、辅助 laravel 应用 SQL 优化
2022-04-21 18:04:00 【guanguans】

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
{
}
}
版权声明
本文为[guanguans]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_36936891/article/details/124294912
边栏推荐
- 2022超星学习通:媒体创意经济、走进东盟、职业生涯提升、音乐鉴赏、突发事件及自救
- What can MCU do? Do you have any interesting works made by MCU or open source hardware
- In cabin visual AI application front loading "increased by nearly 200% year-on-year". Which suppliers lead the Chinese market
- 兼职比工资还高,2022年,月入过万的3个副业
- Logstash ~ configuration file of logstash
- String(一个特殊的数据类型)
- torch.max()的用法
- LogStash~LogStash的多个输入输出
- Live broadcast with goods source code, different writing methods of gradient status bar
- 全新ETL调度批量管理工具 TASKCTL 8.0 最简安装
猜你喜欢

Linux~libc.so.6(GLIBC_2.28)(64bit) 被 redis-5.0.3-1.nfs.x86_64 需要

华为URPF

关于Linq语句

Is it really safe to outsource model training? New research: outsourcers may implant backdoors to control bank lending

STM32单片机内存管理器代码,可直接用于工程

有奖征文 | 重奖5000元,邀您投稿

头条自媒体运营秘籍,坚持下去你就可以打败90%的人

STM32 MCU memory manager code can be directly used in engineering

最基本的JDBC模板及数据库乱码处理

终于完成学生时代的梦想-制作掌机用单片机STM32手把手教你
随机推荐
干货|app自动化测试之Appium 源码分析
Is it really safe to outsource model training? New research: outsourcers may implant backdoors to control bank lending
为什么switch里的case没有break不行
AI + clinical trial | Bayer exploration creates "virtual" control group
mysql汉化-workbench汉化-xml文件
将模型训练外包真的安全吗?新研究:外包商可能植入后门,控制银行放款
LogStash~LogStash命令行可选参数
Logstash ~ output of logstash
What happens when the user sends a request to execute the controller method
Integer面试解析、equals与==的区别
Linux~libc. so. 6 (glibc_2.28) (64bit) is redis-5.0.3-1 nfs. x86_ 64 required
LogStash~LogStash的目录布局
In cabin visual AI application front loading "increased by nearly 200% year-on-year". Which suppliers lead the Chinese market
Educational Codeforces Round 116 (Rated for Div. 2) E. Arena
“工业互联网+安全生产”,提升工业企业安全水平
MySQL localization workbench localization XML file
华为云GaussDB(for Influx)揭密第六期——数据分级存储
LogStash~LogStash的工作原理
接口测试框架实战(二)| 接口请求断言
"Industrial Internet plus safety production" to enhance the safety level of industrial enterprises