Everyone knows the N+1 problem is bad. What is less obvious is where the cost actually lives. I measured it locally and the answer was uncomfortable: almost none of it was in the query.
The test
Two tables in an in-memory SQLite database: 100 authors, 1,000 posts. Then two ways to fetch the author name for every post.
# N+1: one query per post
for pid in range(1, 1001):
cur.execute(
"SELECT a.name FROM posts p JOIN authors a ON a.id = p.author_id WHERE p.id = ?",
(pid,),
)
# one query
cur.execute("SELECT a.name FROM posts p JOIN authors a ON a.id = p.author_id LIMIT 1000")
On a real run (Python 3.10, SQLite 3.37, in-memory database):
n+1 (1,001 queries): 1.08 ms
join (1 query): 0.27 ms
local ratio: 3.98x
Four times slower, and the whole thing still finished in about a millisecond. The video run of the same test used 101 queries instead of 1,001 and measured 6.37 ms against 6.34 ms, a ratio of 1.01x. Different sizes, same story: on a local database the N+1 penalty is small enough to disappear into the noise.
Why the local test lies
An in-memory SQLite query is a function call. There is no socket, no serialisation, no authentication, no network. A thousand of them cost about a millisecond.
The production version has a round trip in front of every one of those queries. A query against a database on the same rack is often around a millisecond; across an availability zone it can be several. The arithmetic is the part worth doing:
- 1,001 queries at 1 ms per round trip is about 1 second of pure waiting, before the database does any work.
- The same data as one query is one round trip.
That is the N+1 problem. Not slow SQL. A thousand conversations where one would do.
There is a second cost that shows up under load even when each round trip is fast: every one of those queries needs a connection from the pool. A page doing 1,001 queries holds a connection for far longer than a page doing 1, and with a handful of concurrent requests the pool is exhausted and the app queues behind itself.
The fix
Two queries instead of 1,001. Fetch the posts, collect the author ids, then fetch those authors in one IN query and join them in memory. Most ORMs call this eager loading (include, with, selectinload, joinedload), and most also offer a way to see when you have forgotten it.
How to find the N+1s in your own code:
- Count queries per request in development and fail the test suite above a budget. Frameworks expose this as a query counter or an event hook.
- Look for a query inside a loop over rows, especially inside a template or a serialiser.
- Watch the pool: a request that suddenly needs dozens of connections is usually an N+1, not a slow query.
What the test does not measure
This test says nothing about whether your indexes are right, and it does not simulate a network. That is exactly why it is useful: it isolates the part of the N+1 problem that has nothing to do with SQL, so the real cost stays visible when you add the network back in.
How this was tested
Real run in Python 3.10 with the standard library sqlite3 module against an in-memory database, timed with time.perf_counter(). The narration in the video is an AI-generated voice; the numbers above came from a terminal. Short video with the same idea: https://youtube.com/shorts/nFwG5ZZOGPA
Top comments (0)