saasscaleup / laravel-n-plus-one-detector
为Laravel应用实时检测和解决N+1查询问题
v1.0.5
2024-08-02 05:01 UTC
Requires
- php: ^7.3|^8.0
- illuminate/support: ^5.5|^6.0|^7.0|^8.0|^9.0|^10.0
README
为Laravel应用实时检测和解决N+1查询问题。提供详细见解、高级通知和丰富的管理仪表板。
非常适合独立开发者和小团队。兼容Laravel 5.5+和PHP 7+。
Youtube · Twitter · Facebook · Buy Me a Coffee
Laravel N+1查询检测器
Laravel N+1查询检测器是一款强大的包,旨在帮助您实时识别和解决N+1查询问题。非常适合独立开发者和小团队,此包通过在影响用户之前捕获低效查询来提高您的应用性能。
✨ 特点
- 实时N+1查询检测:实时识别N+1查询,确保您的应用保持高效。
- 详细的查询见解:获取每个检测到的N+1查询的详细信息,包括文件行、涉及的类和方法。
- 高级通知:通过Slack、webhook或电子邮件接收警报,确保您不会错过任何重要通知。
- 丰富的管理仪表板:在全面且用户友好的仪表板中查看所有N+1警告。
- 适用于团队和独立开发者:专为独立开发者和协作工作的团队设计。
要求
- PHP >= 7
- Laravel >= 5.5
安装
安装composer包(开发环境)
通过Composer - 不推荐在生产环境中使用
composer require --dev saasscaleup/laravel-n-plus-one-detector
发布包的配置、迁移和视图文件
通过运行以下命令发布包的配置、迁移和视图文件
首先尝试此命令
php artisan vendor:publish --provider="Saasscaleup\NPlusOneDetector\NPlusOneDetectorServiceProvider"
如果您得到此结果
No publishable resources for tag [].
Publishing complete.
然后尝试此操作
php artisan vendor:publish
然后输入代表提供商的数字(以下示例中的数字为14)
[13] Provider: Saasscaleup\LogAlarm\LogAlarmServiceProvider
[14] Provider: Saasscaleup\NPlusOneDetector\NPlusOneDetectorServiceProvider
> 14
运行迁移命令
运行php artisan migrate
以创建nplusone_warnings
表。
php artisan migrate
运行路由缓存命令
运行php artisan migrate
以确保路由缓存将被清除,因为我们正在添加新的路由/n-plus-one-dashboard
。
php artisan route:cache
配置
您可以通过编辑config/n-plus-one.php
文件来配置该包。此文件允许您设置检测N+1查询的阈值、通知偏好设置等。
<?php return [ // Whether or not to enable the N+1 Detector 'enabled' => env('NPLUSONE_ENABLED', true), // The number of queries below which no alert will be triggered 'queries_threshold' => env('NPLUSONE_QUERIES_THRESHOLD', 50), // The number of queries below which no detector will be triggered 'detector_threshold' => env('NPLUSONE_DETECTOR_THRESHOLD', 10), // The number in minutes a n+1 query will be stored in memory before being discarded. So it won't repeat itself 'cache_lifetime' => env('NPLUSONE_CACHE_LIFETIME', 14400), // 10 days // Slack webhook url for N + 1 Detector 'slack_webhook_url' => env('NPLUSONE_SLACK_WEBHOOK_URL', ''), // Custom webhook url for N + 1 Detector 'custom_webhook_url' => env('NPLUSONE_CUSTOM_WEBHOOK_URL', ''), // notification email address for N + 1 Detector 'notification_email' => env('NPLUSONE_NOTIFICATION_EMAIL', 'admin@example.com'), // also possible: 'admin@example.com,admin2@example.com' // notification email subject for N + 1 Detector 'notification_email_subject' => env('NPLUSONE_NOTIFICATION_EMAIL_SUBJECT', 'N+1 Detector Notification'), // Dashboard Middleware for N + 1 Detector 'dashboard_middleware' => env('NPLUSONE_DASHBOARD_MIDDLEWARE', ['web', 'auth']), // Dashboard Pagination for N + 1 Detector 'dashboard_records_pagination' => env('NPLUSONE_DASHBOARD_RECORDS_PAGINATION', 10), ];
用法
实时检测
该包自动监听您的数据库查询,并实时检测N+1问题。当检测到N+1查询时,它会记录查询详情,并可选择发送通知。
管理仪表板
访问丰富的管理仪表板以查看所有N+1警告
Route::get('/n-plus-one-dashboard', [NPlusOneDashboardController::class, 'index'])->name('n-plus-one.dashboard');
仪表板提供了对所有检测到的N+1查询的全面查看,包括SQL语句、发生次数、位置和建议的修复。
通知
配置通知以通过Slack、webhook或电子邮件发送。在config/n-plus-one.php
文件中设置您的通知偏好设置,以了解您的应用中的N+1问题。
// Slack webhook url for N + 1 Detector 'slack_webhook_url' => env('NPLUSONE_SLACK_WEBHOOK_URL', ''), // Custom webhook url for N + 1 Detector 'custom_webhook_url' => env('NPLUSONE_CUSTOM_WEBHOOK_URL', ''), // notification email address for N + 1 Detector 'notification_email' => env('NPLUSONE_NOTIFICATION_EMAIL', 'admin@example.com'), // also possible: 'admin@example.com,admin2@example.com'
Slack通知
Webhook通知
电子邮件通知
高级功能
详细查询洞察
此软件包为每个检测到的N+1查询提供详细的洞察,包括涉及的类和方法。这有助于您快速定位问题的根源并实施修复。
许可证
有关更多信息,请参阅MIT许可证。
支持🙏😃
如果您喜欢这个教程,并想支持我的频道,那么我会继续发布精彩内容,帮助您成为拥有出色云技能的理想开发者...如果您能这样做,我会非常感激
感谢您的支持 :)