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 applic...
WordPress Development Services by Arun Tyagi
Professional WordPress development extends far beyond theme installation and plugin configuration. S...
Professional Web Development Services Using Laravel, PHP & WordPress
Discover professional web development services using Laravel, PHP, and WordPress. Learn how custom,...