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
OPcache
in PHP. - Use
php artisan config:cache
androute: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
CSS Specificity: Layers vs BEM vs Utility Classes
Jun 26, 2025
CSS Specificity: Cascade Layers vs BEM vs Utility Classes This article compares three approaches to managing CSS specificity — BEM, utilityโf...
React Labs: View Transitions & Activity
Jun 17, 2025
React Labs: View Transitions & Activity Published April 23, 2025 by Ricky Hanlon. React Labs is sharing two new experimental featu...
Comparison between Scopes + Traits ร UseEloquentBuilder in Laravel
Jul 13, 2025
Laravel provides multiple ways to write reusable query logic. The two most common approaches are using Scopes with Traits or the newer #[UseEloquentBu...
Task Reminder with Laravel & MongoDB
Jun 30, 2025
๐ Building a Task Reminder App This guide shows how to set up a Laravel app using MongoDB to implement a task reminder system with authentication,...
Using Web Components the Smart Way
Jul 06, 2025
Using Web Components the Smart Way A lot of developers assume Web Components are meant to replace full SPA frameworks like React or Vue. But in rea...
How OAuth Works
Jun 29, 2025
How OAuth Works OAuth is a protocol that allows third-party applications to access user data without sharing passwords. It's the backbone of secure a...
