nunomaduro/laravel-optimize-database
Laravel package by Nuno Maduro that inspects the queries your app actually runs and adds the missing database indexes for you. It records query patterns, then generates migrations to speed up slow lookups, making database optimization largely automatic in Laravel projects.
Minimal steps to get value from nunomaduro/laravel-optimize-database:
composer require nunomaduro/laravel-optimize-database --dev
optimize-database-migrations):
php artisan vendor:publish --tag="optimize-database-migrations"
database/migrations. They are plain Laravel migrations that add indexes and foreign keys to common tables (e.g., jobs, failed_jobs, password_resets, personal_access_tokens).php artisan migrate
Where to look first: The published migrations themselves—they are the entire product. Read each one to see which columns get indexes and which constraints are added. Also skim the package README for supported Laravel/database versions and any publish-tag quirks.
First use case: On a new Laravel project, after your base migrations have created the standard tables, publish and run these migrations to add missing performance indexes and integrity constraints. On an existing project, run them in staging first to catch data-integrity issues before production.
users.email and personal_access_tokens.token, for example). Add indexes for your own frequently queried columns. Adjust foreign-key cascade rules to match your domain.2021_01_01_000000), they may run before your own base migrations. Rename the published files to a timestamp after your latest base migration, or use the package’s config if one is provided.php artisan migrate step in deployment will apply them. Use php artisan migrate --pretend in staging to review the exact SQL before touching production.php artisan migrate:fresh in your test suite to verify the migrations work on a clean database. Use a separate test database or transactions to keep tests isolated.ALGORITHM=INPLACE; for PostgreSQL, use CREATE INDEX CONCURRENTLY (the package may not do this automatically). For SQLite, ensure foreign keys are enabled in your connection config.2024_01_01_000000_...) after your base migrations.nullable and backfill, or skip that migration and add it later. Always test on a copy of production data.users.email unique, personal_access_tokens.token unique). The package may add overlapping indexes. Review and remove duplicates to avoid unnecessary write overhead.pt-online-schema-change, gh-ost) or run during maintenance. The published migrations are plain Laravel, so you can modify them to use raw SQL with CONCURRENTLY for PostgreSQL.php artisan migrate --pretend to see the exact SQL. After running, verify indexes with SHOW INDEX FROM table (MySQL) or \d table (PostgreSQL). Laravel’s db:table command (Laravel 8+) can also show indexes.->cascadeOnDelete()). The package may not be actively maintained, so test with your Laravel version.composer.json. If you want to receive future updates, keep it as a dev dependency and re-publish periodically to see new migrations.How can I help you explore Laravel packages today?