The N+1 Query Problem and Eager Loading
The classic ORM bug where a loop issues one query per item instead of one batched query — how to detect it and how to fix it.
The N+1 Query Problem and Eager Loading
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- The N+1 problem is one query to fetch a list, followed by one additional query per item in that list to fetch related data — a cost that scales with the size of the list, often invisibly, because the code that causes it looks completely ordinary.
- An ORM's lazy loading is almost always the root cause: accessing a related object looks like reading a plain field, but the first time you touch it, the ORM quietly fires a new query.
- You detect it by counting or logging queries per request in development, not by watching response times, because each individual query in an N+1 chain is usually fast on its own — it's the count that's the problem, not any single query's speed.
- The fix is always some form of telling the ORM to fetch the related data up front — eager loading via a join, or a second batched query using an IN clause — instead of letting it fetch one row at a time inside a loop.
A bug that looks like clean code
Here’s the setup that produces this bug almost every time: you fetch a list of records, then loop over them and access something related to each one.
posts = Post.objects.all()
for post in posts:
print(post.author.name)
This reads like perfectly ordinary code. It’s also, in most ORMs by default, a performance bug. Fetching posts is one query. But post.author on each iteration triggers the ORM’s lazy loading — it doesn’t have the author loaded yet, so the first time you touch that attribute, it quietly goes back to the database and fetches it. One query per post. Twenty posts means twenty-one total queries where one or two would have done the job. That’s the “N+1”: one query for the list, N more for each item’s related data.
The reason this bug is so common, and so easy to miss in code review, is that nothing about the code’s appearance gives it away. There’s no visible loop making database calls — post.author.name looks like reading a plain attribute. The query is hidden entirely behind the ORM’s convenience.
Why it scales badly in exactly the way that bites you in production
A list of 10 items with this bug is barely noticeable — 11 queries is cheap. The problem is that this code path typically lives on a page like “list of posts” or “list of orders,” and the whole point of such a page is that the list grows over time. Twenty items in dev, two thousand in production six months later. The query count scales linearly with the size of the list, so the bug gets worse exactly as your product succeeds and your data grows — which is also exactly the moment nobody wants a surprise.
Detecting it: watch the count, not the clock
The standard mistake in catching this is watching response time. Don’t — each individual query in an N+1 chain is small and typically fast, so a page doing fifty of them can still “feel” fine in casual testing, especially against a small local database with low latency to it. The reliable signal is the number of queries a single request issues, not how long any of them take.
In practice this means turning on query logging in development and actually reading it, or using a tool built for exactly this: Django’s django-debug-toolbar shows a query count and flags duplicates per request; Rails has bullet, which detects N+1 patterns automatically and warns in the console or logs; most ORMs expose some form of query-counting middleware for tests, letting you write an assertion like “this endpoint must not issue more than 3 queries” and catch a regression in CI before it ever reaches production. The fingerprint to look for in logs is the same query shape repeated many times with only the ID parameter changing — that’s N+1 announcing itself.
The fix: tell the ORM to load it up front
Every fix for N+1 comes down to the same idea: stop asking for related data one row at a time, and ask for it all at once, before the loop starts. There are two common shapes this takes, and the distinction matters.
Join-based eager loading (Rails’ eager_load, Django’s select_related, JPA/Hibernate’s JOIN FETCH) folds the related data into the same query using a SQL join. This is one round trip total, but for one-to-many relationships it can return duplicate copies of the parent row — one per matching related row — which the ORM then has to de-duplicate back into objects on its side.
Batched eager loading (Rails’ preload, Django’s prefetch_related, Laravel’s with(), Prisma’s include) runs a second, separate query that fetches all the related rows in one shot using a WHERE id IN (...) clause, then stitches the results back onto the right parent objects in application code. This avoids the duplicate-row issue and is usually the better default for one-to-many and many-to-many relationships, at the cost of one extra round trip instead of zero.
The video embedded above walks through exactly this fix inside a real Laravel codebase — spotting the N+1 pattern, then reaching for with() to collapse it into two queries. The underlying lesson generalizes directly to Django’s prefetch_related, Rails’ includes, or the equivalent call in whatever ORM you’re using; the API name changes, the fix doesn’t.
Beyond the ORM: the dataloader pattern
The same problem shows up outside a traditional ORM loop, most visibly in GraphQL APIs: a resolver for a field like author gets called once per post in the result set, by design, with no loop in your code to even eager-load around. The standard fix there is a DataLoader-style batching layer: instead of each resolver call hitting the database immediately, it registers its request and the library collects every pending request made within the same tick of execution, then issues one batched query for all of them together. It’s the same underlying fix as eager loading — one query instead of many — implemented at a different layer, because GraphQL’s per-field resolution model doesn’t give you a loop to eager-load around in the first place.
Check yourself
In plain terms, what is the N+1 query problem?
It's the pattern where fetching a list of N items takes one query, and then fetching each item's related data takes one more query per item — N additional queries — so a page showing 50 posts with their authors can silently issue 51 queries instead of 2.
Why does an ORM's lazy loading specifically cause this, when the code doesn't look like it's doing anything wrong?
Lazy loading makes accessing a related object (like post.author) look exactly like reading an ordinary field, but the ORM defers the actual query until that access happens — so a loop that touches post.author for every post in a list triggers one query per iteration, and nothing in the code's appearance signals that.
Why is watching for slow response times the wrong way to detect an N+1 problem?
Each individual query in the chain is usually small and fast on its own, so page response time can look acceptable even while the request is making dozens of unnecessary round trips to the database — the signal to watch for is the query count per request, which is why turning on query logging (or a tool that counts queries) in development catches it far more reliably than watching for a slow page.
What's the difference between fixing N+1 with a join versus fixing it with a second batched query?
A join-based fix (like Rails' eager_load or JPA's JOIN FETCH) pulls the related data into the same single query, which can duplicate the parent row for each related row returned; a batched second-query fix (like Rails' preload, Django's prefetch_related, or Laravel's with()) runs exactly one extra query using a WHERE id IN (...) clause and stitches the results together in application code, avoiding duplication but costing one additional round trip.
How does a DataLoader-style pattern generalize the same fix beyond a single ORM's eager-loading API?
DataLoader batches and caches individual load requests that happen within the same tick of execution — so instead of one query per item, every pending request for related data made during that window gets collected and resolved in a single batched call — which is why it's the standard fix for N+1 in GraphQL resolvers, where the ORM's own eager-loading hints often don't apply cleanly.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.