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

1.0.0 2025-10-18 18:05 UTC

This package is auto-updated.

Last update: 2025-10-18 18:07:00 UTC


README

Monitor your Eloquent/DB queries in Laravel and get actionable index suggestions to optimize performance.

License: MIT Laravel Packagist

📘 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 slow
  • usage_threshold: Number of times a column must be used to trigger an index suggestion
  • auto_generate_migrations: If true, 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!

  1. Fork the repository
  2. Create a feature branch: git checkout -b feature/new-feature
  3. Commit your changes: git commit -m "Add new feature"
  4. Push to your fork: git push origin feature/new-feature
  5. 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.