Laravel

Laravel Query Optimization with Indexes

January 16, 2026 1 min read 20 views

Optimize with indexes:

Add Index in Migration

$table->index('email');
$table->index(['user_id', 'created_at']);
$table->unique('slug');

Foreign Key Index

$table->foreignId('user_id')->constrained()->index();

Drop Index

$table->dropIndex(['email']);
$table->dropUnique(['slug']);

Explain Query

User::where('email', $email)->explain();

Best Practices

- Index frequently queried columns
- Index foreign keys
- Use composite indexes for multi-column queries
- Avoid over-indexing
Share this post:

Related Posts

Comments (0)

Please log in to leave a comment. Log in

No comments yet. Be the first to comment!