One of the most common โ and most damaging โ performance issues in Laravel applications is the N+1 query problem. It rarely shows up in local development with a handful of test rows, but it becomes very visible in production once tables have real volume.
What is an N+1 query?
It happens when you load a collection of models and then lazy-load a relationship inside a loop, triggering one additional query per row instead of a single, efficient join or eager load.
How I found it
- Enabled Laravel Debugbar in a staging environment mirroring production data volume
- Looked for repeated near-identical queries in the query log
- Traced them back to a Blade view looping over orders and lazy-loading
$order->customerand$order->items
The fix
Switching to eager loading with Order::with(['customer', 'items.product']) collapsed hundreds of queries into a handful. Combined with targeted indexes on foreign keys, this dropped page load time by roughly 35% on the affected admin screens.
Takeaway
If a page feels slow under real data but fine in development, check your query log first. N+1 problems are usually invisible until they aren’t โ and they’re almost always a one-line fix once you find them.
Leave a Reply