Is Laravel Slow? Optimize Queries & Indexes for Maximum Performance
July 20, 2025A detailed, example-rich guide to avoid slowdowns in Laravel apps by optimizing data retrieval and employing indexing smartly.
1. π§ Fetch Only What You Need
Loading every column and row with DB::table('users')->get() can clog resources. Instead:
$users = DB::table('users')
->select('id', 'name', 'email')
->where('status', 'active')
->limit(50)
->get();
With Eloquent:
$users = User::select('id','name','email')
->where('status','active')
->take(50)
->get();
Why? Less memory, fewer bytes transferred, faster response times.
2. π Leverage Database Indexing
If filtering on columns like customer_id or status, adding indexes speeds up searches:
Schema::table('orders', function (Blueprint $table) {
$table->index('customer_id');
$table->index(['status', 'created_at']); // composite index
});
Run php artisan migrate to apply. Only add indexes where they help in WHERE, JOIN, or ORDER BY. Avoid over-indexing to prevent slow writes.
3. π οΈ Profile and Debug Queries
During development:
DB::enableQueryLog();
// run query then:
dd(DB::getQueryLog());
Try tools like Laravel Debugbar or Telescope, or run raw SQL with EXPLAIN to verify index use:
EXPLAIN SELECT * FROM orders WHERE customer_id = 5;
4. β Checklist for Fast Laravel Queries
- Select only fields you need (
select()). - Limit rows with
limit()ortake(). - Add proper indexes on WHERE/JOIN columns.
- Profile with Debugbar/Telescope.
- Avoid loading unused relationships (prevent N+1 queries).
5. β¨ Advanced Tips & Bonus Tricks
Eager vs Lazy Loading: Use with() to avoid N+1:
$posts = Post::with('comments')->get();
Chunking big datasets:
Post::chunk(100, function($posts) {
foreach ($posts as $post) {
// process...
}
});
Cache heavy queries:
$posts = Cache::remember('all_posts', 60, function() {
return Post::with('comments')->get();
});
Use raw SQL when needed:
$rows = DB::select('SELECT * FROM orders WHERE status = ?', ['active']);
Monitor: Log every query:
DB::listen(function($query){
Log::info("SQL: {$query->sql} - Bindings: " . implode(',', $query->bindings) . " - Time: {$query->time}ms");
});
6. π Scale & Optimize Further
- Enable
OPcachein PHP. - Use
php artisan config:cacheandroute:cache. - Pick fast cache stores (Redis, Memcached).
- Consider horizontal scaling (multiple servers).
- Profile regularly; optimize early and often.
By writing precise queries, indexing smartly, and profiling well, your Laravel app will stay fast and scalable. Performance starts at your queries.
Blog
Laravel Queue & Job System: From Table Creation to Production Deployment
Jul 01, 2025
π Laravel Queue & Job System We’re gonna walk you through Laravel queues from setup to deploying in production using Supervisor. Step 1...
Analyze Laravel Projects with Introspect
Jul 01, 2025
Analyze Laravel Codebases with Laravel Introspect If you’re doing a complex refactor or building dev tools, Laravel Introspect helps you quer...
Stop Copy-Pasting Code! Learn How to Use Traits in Laravel the Right Way
Jul 01, 2025
π« Stop Copy-Pasting Code! Ever duplicated slug logic or logging across multiple models? Laravel's Traits got your back. 1. What’s a Trait?...
Explore the Most Powerful Modern Laravel Tools: Inertia.js, View Creators, and HLS β Step by Step
Jul 27, 2025
Here’s a complete breakdown of essential tools to level up your Laravel development: Inertia.js v2, View Creators, and the Laravel HLS package...
Color Everything in CSS β Simple Guide
Jun 26, 2025
Color Everything in CSS – Simple Guide Today we’re diving into CSS colors: how to define them, especially with modern methods like lab(...
Mastering LaravelΒ 12 Conditional Validation
Jul 07, 2025
Mastering Laravel 12 Conditional Validation Laravel 12's validation system is super powerful, and conditional validation makes your forms...