Laravel DB Monitor β Real-Time Database Performance Monitoring, Slow Query Detection & N+1 Analysis
Laravel DB Monitor is a real-time database performance monitoring package for Laravel applications.
It automatically detects slow queries, N+1 query problems, and missing indexes, then suggests SQL optimizations and can even generate index migrations for you.
A production-ready Laravel performance monitoring tool for Laravel 10, 11 & 12.
- π’ Detect slow database queries in real time
- π Identify N+1 query problems automatically
- π Suggest missing indexes based on real usage
- π‘ Show actionable fix suggestions
- π§ Generate index migrations automatically
- π¬ Send alerts via Email or Slack
- π Clean CLI reporting (
db:report) - π§Ή Auto-prune old logs using Laravelβs Prunable trait
- β‘ Works with MySQL, PostgreSQL, SQLite
composer require benjdiasaad/laravel-db-monitorPublish config & migrations:
php artisan vendor:publish --tag=db-monitor-config
php artisan vendor:publish --tag=db-monitor-migrations
php artisan migrate->withMiddleware(function (Middleware $middleware) {
$middleware->web(append: [
\BenjdiaSaad\DbMonitor\Http\Middleware\MonitorQueries::class,
]);
})protected $middlewareGroups = [
'web' => [
// ...
\BenjdiaSaad\DbMonitor\Http\Middleware\MonitorQueries::class,
],
];Add to .env:
DB_MONITOR_ENABLED=true
DB_MONITOR_SLOW_THRESHOLD=500
DB_MONITOR_N1_THRESHOLD=10
DB_MONITOR_INDEX_THRESHOLD=50
DB_MONITOR_NOTIFY=admin@yourapp.com
DB_MONITOR_RETENTION=7Config file: config/db-monitor.php
return [
'enabled' => env('DB_MONITOR_ENABLED', true),
'slow_query_threshold_ms' => env('DB_MONITOR_SLOW_THRESHOLD', 500),
'n_plus_one_threshold' => env('DB_MONITOR_N1_THRESHOLD', 10),
'missing_index_min_occurrences' => env('DB_MONITOR_INDEX_THRESHOLD', 50),
'store_queries' => env('DB_MONITOR_STORE_QUERIES', true),
'retention_days' => env('DB_MONITOR_RETENTION', 7),
'notify' => env('DB_MONITOR_NOTIFY', null),
'notification_channels' => ['mail'],
'exclude_paths' => [
'telescope/*',
'_debugbar/*',
'horizon/*',
'livewire/*',
],
];php artisan db:reportExample output:
DB Monitor Report β Last 24 hours
βΆ SLOW QUERY
Slow query detected: 2300ms
Path: api/orders
π‘ Suggestion: Use select() instead of SELECT *
π‘ Add index:
php artisan db:fix --table=orders --column=user_id
βΆ N+1 QUERY
Same query executed 47 times
π‘ Suggestion: Model::with('user')->get()
βΆ MISSING INDEX
Column orders.user_id used in 230 queries
π‘ Auto-generate migration:
php artisan db:fix --table=orders --column=user_id
php artisan db:report --hours=48
php artisan db:report --severity=critical
php artisan db:report --type=n_plus_oneGenerate migration:
php artisan db:fix --table=orders --column=user_id
php artisan migrateFix all at once:
php artisan db:fix --all
php artisan migratephp artisan db:analyze --hours=24php artisan db:clear --days=7
php artisan db:clear --allEnable email alerts:
DB_MONITOR_NOTIFY=admin@yourapp.comEnable Slack notifications in config/db-monitor.php:
'notification_channels' => ['slack'],Configure Slack in config/services.php:
'slack' => [
'notifications' => [
'bot_user_oauth_token' => env('SLACK_BOT_USER_OAUTH_TOKEN'),
'channel' => env('SLACK_BOT_USER_DEFAULT_CHANNEL'),
],
],use BenjdiaSaad\DbMonitor\Facades\DbMonitor;
$findings = DbMonitor::runDetectors($queries);
$analysis = DbMonitor::analyzeStoredLogs(hours: 48);| Command | Description |
|---|---|
db:report |
Show database health report |
db:report --hours=48 |
Report for last 48 hours |
db:report --severity=critical |
Only critical issues |
db:report --type=n_plus_one |
Only N+1 findings |
db:analyze |
Analyze stored logs |
db:fix --table=x --column=y |
Generate index migration |
db:fix --all |
Fix all missing indexes |
db:clear --days=7 |
Clear old logs |
db:clear --all |
Clear everything |
Stores every captured query per request.
- sql
- bindings
- duration_ms
- connection
- request_id
- request_path
Stores detected issues.
- type (
slow_query,n_plus_one,missing_index) - severity (
warning,critical) - message
- context (json)
- request_path
- notified
Both tables use Laravel's Prunable trait.
Run manually:
php artisan model:pruneSchedule daily:
Schedule::command('model:prune')->daily();- PHP 8.2+
- Laravel 10, 11, or 12
- Any Laravel-supported database
Unlike traditional profilers, Laravel DB Monitor:
- Stores historical query patterns
- Detects recurring performance issues
- Suggests fixes automatically
- Can generate index migrations for you
- Works in production environments
It helps you catch database problems before your users experience slow pages.
MIT β free for personal and commercial use.
Benjdia Saad
GitHub: https://github.com/benjdiasaad
Built to help Laravel developers optimize database performance before it becomes a problem.