jumamiller/optimize-laravel-query
Composer 安装命令:
composer require jumamiller/optimize-laravel-query
包简介
An AI powered optimizer for laravel eloquent and query builder
README 文档
README
Optimize laravel DB or eloquent read queries using openAI API, and get insights on how you can make your queries faster
Installation
First, install the package using composer
composer require jumamiller/optimize-laravel-query --dev
Then, publish openAI configuration file
php artisan vendor:publish --provider="OpenAI\Laravel\ServiceProvider"
Lastly, add the openAPI key to your .env file
OPENAI_API_KEY=sk-...
and that's it, you are ready to go
Usage
this package can help you optimize your eloquent or DB queries using openAI
User::query() //can be applied to `DB facade` as well (see tests) ->select('users.id', 'users.name') ->whereHas('roles', function ($query) { $query->where('name', 'author'); }) ->whereHas('posts', function ($query) { $query->where('title', 'Awesome post'); })->optimize()->get();
the package has the following methods
optimize()
This method is responsible for optimizing the query and return an instance of the Optimizer class.
User::query() ->select('users.id', 'users.name') ->whereHas('roles', function ($query) { $query->where('name', 'author'); }) ->whereHas('posts', function ($query) { $query->where('title', 'Awesome post'); })->optimize(); //old query => select `id`, `name` from `users` where exists (select * from `roles` inner join `role_user` on `roles`.`id` = `role_user`.`role_id` where `users`.`id` = `role_user`.`user_id` and `type` = ?) and exists (select * from `posts` where `users`.`id` = `posts`.`user_id` and `title` = ?) //optimized query => SELECT `id`, `name` FROM `users` INNER JOIN `role_user` ON `users`.`id` = `role_user`.`user_id` INNER JOIN `roles` ON `roles`.`id` = `role_user`.`role_id` INNER JOIN `posts` ON `users`.`id` = `posts`.`user_id` WHERE `type` = ? AND `title` = ?
toSql()
this method will return the optimized query as a string
User::query() ->select('users.id', 'users.name') ->whereHas('roles', function ($query) { $query->where('type', 'author'); }) ->whereHas('posts', function ($query) { $query->where('title', 'Awesome post'); })->optimize()->toSql(); //output => SELECT `id`, `name` FROM `users` INNER JOIN `role_user` ON `users`.`id` = `role_user`.`user_id` INNER JOIN `roles` ON `roles`.`id` = `role_user`.`role_id` INNER JOIN `posts` ON `users`.`id` = `posts`.`user_id` WHERE `type` = ? AND `title` = ?
get()
this method will run the optimized query that is generated by optimize() method and return the result
⚠️ make sure to know the query you are running before using this method, as it will run the optimized query and not the original query
User::query() ->select('users.id', 'users.name') ->whereHas('roles', function ($query) { $query->where('type', 'author'); }) ->whereHas('posts', function ($query) { $query->where('title', 'Awesome post'); })->optimize()->get()->toArray(); //output => ['id' => '1', 'name' => 'omar']
explain()
this method will return a key-value array that contains the optimized query, the reasoning behind performing such optimization, and suggestions to manually optimize the query even further
User::query() ->select('users.id', 'users.name') ->whereHas('roles', function ($query) { $query->where('type', 'author'); }) ->whereHas('posts', function ($query) { $query->where('title', 'Awesome post'); })->optimize()->explain();
the array format will be
[ 'optimizedQuery' => 'SELECT `id`, `name` FROM `users` INNER JOIN `role_user` ON `users`.`id` = `role_user`.`user_id` INNER JOIN `roles` ON `roles`.`id` = `role_user`.`role_id` INNER JOIN `posts` ON `users`.`id` = `posts`.`user_id` WHERE `type` = ? AND `title` = ?' // the optimized query 'reasoning' => 'This query optimizes the original query by using JOINs to reduce the number of subqueries and improve the performance of the query. By using JOINs, the query can access the data from multiple tables in a single query, instead of having to make multiple subqueries.' //the reasoning behind performing such optimization, 'suggestions' => 'It may be beneficial to add an index on the `type` and `title` columns to further improve the performance of the query.' //suggestions to manually optimize the query even further ]
Credits
License
The MIT License (MIT). Please see License File for more information.
Inspiration
jumamiller/optimize-laravel-query 适用场景与选型建议
jumamiller/optimize-laravel-query 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 14 次下载、GitHub Stars 达 1, 最近一次更新时间为 2023 年 07 月 18 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「database」 「optimization」 「laravel」 「Performace」 「openai」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 jumamiller/optimize-laravel-query 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 jumamiller/optimize-laravel-query 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 jumamiller/optimize-laravel-query 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Dibi is Database Abstraction Library for PHP
Image optimization / compression library. This library is able to optimize png, jpg and gif files in very easy and handy way. It uses optipng, pngquant, pngcrush, pngout, gifsicle, jpegoptim and jpegtran tools.
Store your language lines in the database, yaml or other sources
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
Image optimization widget for Yii2 Framework with auto WebP/AVIF image format generation from PNG/JPG files.
Optimize your email templates' styles via native Laravel templating mechanism.
统计信息
- 总下载量: 14
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 9
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-07-18