编程

[Laravel 扩展包]Laravel Slower——使用 AI 优化 Eloquent 查询

274 2024-05-21 04:13:00

Laravel Slower 包是为那些希望提高应用程序性能的Laravel开发人员设计的。此包识别慢速查询,并建议进行优化,如索引和其他改进。
取决于你如何配置应用调度程序,你可以每天运行以下命令来分析和清理旧记录:

php artisan slower:clean /*{days=15}  Delete records older than 15 days.*/
php artisan slower:analyze /*Analyze the records where is_analyzed=false*/
  • 使用 slower:analyze 命令创建的建议行为,存储在该包创建的数据库表中,你可以在 AI 分析完成后查看该表。作为 AI 分析的一部分,该软件包的主要功能包括:
    可配置的慢速阈值
  • 可配置的 AI 模型,如 Chat GPT-4
  • 禁用 AI 并只记录慢速查询

用于 AI 可配置提示
禁用慢速查询分析
README  包括一个示例分析,可帮助你直观地了解此软件包的预期内容:

/*
select count(*) as aggregate
from "product_prices"
where "product_id" = '1'
  and "price" = '0'
  and "discount_total" > '0'
*/
 
dd($model->recommendation);
 
/*
Indexing: Effective database indexing can significantly speed up query performance. For your query, consider adding a combined (composite)
index on product_id, price, and discount_total. This index would work well
because the where clause covers all these columns.
*/
 
/*
CREATE INDEX idx_product_prices
ON product_prices (product_id, price, discount_total);
*/

更多该包信息,可查询其在 Github 上的源码