sagar-s-bhedodkar / laravel-index-advisor
Monitor Eloquent/DB queries in your Laravel application and suggest scaffold migrations for missing indexes.
Installs: 2
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 0
pkg:composer/sagar-s-bhedodkar/laravel-index-advisor
Requires
- php: ^8.1
- illuminate/support: ^9.0|^10.0|^11.0|^12.0
Requires (Dev)
- mockery/mockery: ^1.5
- orchestra/testbench: ^7.8|^8.0|^9.0
- phpunit/phpunit: ^9.5 || ^10.0
README
Monitor your Eloquent/DB queries in Laravel and get actionable index suggestions to optimize performance.
📘 Table of Contents
🚀 Introduction
Laravel Index Advisor is a Laravel package that monitors your database queries and provides intelligent suggestions for missing indexes. It helps developers identify slow or frequently-used queries and optimize database performance with actionable recommendations.
✨ Features
- 📊 Tracks Eloquent and DB queries automatically
- ⏱️ Detects slow queries using a configurable threshold
- 🛠️ Suggests indexes for columns used in WHERE clauses
- 💾 Caches queries to analyze repeated usage
- 🔄 Optional migration generation for suggested indexes
- ⚙️ Artisan commands for query management
- ✅ Production-ready and configurable
⚙️ Installation
Require the package via Composer:
composer require sagar-s-bhedodkar/laravel-index-advisor:@dev
The package auto-discovers itself; no manual registration is required.
Publish the configuration file (optional):
php artisan vendor:publish --tag=config
🛠 Configuration
The configuration file config/index-advisor.php
allows you to customize the behavior:
return [ 'enabled' => true, 'slow_query_threshold_ms' => 200, 'ignore_tables' => ['migrations', 'jobs', 'failed_jobs'], 'ignore_columns' => ['id', 'created_at', 'updated_at'], 'cache_key' => 'index_advisor:queries', 'cache_ttl' => 86400, 'max_storage' => 1000, 'usage_threshold' => 5, 'auto_generate_migrations' => false, 'cache_driver' => 'file', 'min_rows' => 50, ];
slow_query_threshold_ms
: Threshold for marking a query as slowusage_threshold
: Number of times a column must be used to trigger an index suggestionauto_generate_migrations
: Iftrue
, automatically generate migration stubs for missing indexes
🧠 Usage
Record Queries
Laravel Index Advisor automatically records queries during runtime.
Analyze Queries
To get index suggestions:
use SagarSBhedodkar\IndexAdvisor\Facades\IndexAdvisor; $suggestions = IndexAdvisor::analyse(); return response()->json($suggestions);
Generate Migration Stub
$stub = IndexAdvisor::generateMigrationStub($suggestions); file_put_contents(database_path('migrations/' . now()->format('Y_m_d_His') . '_add_indexes.php'), $stub);
Clear Stored Queries
IndexAdvisor::clearStored();
🧰 Artisan Commands
php artisan index-advisor:generate-migration
— Generate a migration for suggested indexes- Additional commands may be added for managing cache and recorded queries
🤝 Contributing
Contributions are welcome!
- Fork the repository
- Create a feature branch:
git checkout -b feature/new-feature
- Commit your changes:
git commit -m "Add new feature"
- Push to your fork:
git push origin feature/new-feature
- Submit a Pull Request 🎉
📄 License
This package is open-sourced software licensed under the MIT license.
👨💻 Author
Sagar Sunil Bhedodkar 📧 sagarbhedodkar456@gmail.com 🌐 GitHub Profile
Made with ❤️ for Laravel developers who want actionable index suggestions and optimized database performance.