Laravel n+1 query optimisation fix
The Problem: Slow SaaS Application & High Database Load
While working on a SaaS application, I noticed something critical β pages that should load in milliseconds were taking 2β3 seconds.
At first glance, everything looked fine. Clean code, optimized queries, proper indexing. But the database load told a different story.
After deeper inspection, the issue was clear:
π Laravel N+1 Query Problem
π What is the N+1 Problem in Laravel?
The N+1 problem occurs when your application executes:
- 1 query to fetch main records
- N additional queries to fetch related data
Example
$users = User::all();
foreach ($users as $user) {
echo $user->posts;
}
This results in:
- 1 query for users
- N queries for posts
π Total = N+1 queries (bad for performance)
β οΈ Real Impact (Production Case)
In my SaaS project:
- Users loaded: 100
- Queries executed: 101+
- Page load time: 2.4 seconds
This was a serious scalability issue.
β Step 1: Diagnose Using Laravel Debugbar
To confirm the issue, I used Laravel Debugbar.
It clearly showed:
- Repeated queries
- Duplicate database calls
- High query count
This is the fastest way to detect N+1 issues in development.
β‘ Step 2: Fix Using Eager Loading (
with()
)
The solution is simple but powerful β Eager Loading.
$users = User::with('posts')->get();
foreach ($users as $user) {
echo $user->posts;
}
Now Laravel fetches everything in just 2 queries:
- 1 for users
- 1 for posts
π Before vs After Optimization
| Scenario | Query Count | Load Time
| Before Fix | 101+ queries | 2.4 sec
| After Fix | 2 queries | 400 ms
π ~90% reduction in queries
π§ Pro Tips for Advanced Optimization
- Always use with() for relationships
- Use withCount() when only counts are needed
- Avoid lazy loading in loops
- Use query logging in production debugging
- Combine with caching for maximum performance
π When to Worry About N+1
You should fix this immediately if:
- Your app is slow under load
- Queries increase with data size
- Server CPU usage is high
πΌ Need Help with Laravel Performance?
If youβre facing performance issues, slow APIs, or scaling problems β I can help.
π Check my Laravel development services here:
https://aruntyagi.com/laravel-development
I specialize in:
- Performance optimization
- SaaS architecture
- Scalable backend systems
- AI-powered development
π Conclusion
The N+1 problem is one of the most common β and most damaging β performance issues in Laravel applications.
The good news?
π Itβs easy to fix once you know where to look.
By applying eager loading and proper debugging, you can drastically improve performance and scalability.
Related Posts
Hiring a Laravel & React.js Developer in Dubai and Gurugram: Build Scalable Digital Solutions
In todayβs fast-paced digital world, businesses need robust, scalable, and future-ready web applicat...
WordPress Development Services by Arun Tyagi
Professional Web Development Services Using Laravel, PHP & WordPress
Discover professional web development services using Laravel, PHP, and WordPress. Learn how custom,...