The N+1 Query Problem in SQL
The quiet performance killer hiding in ORMs and loops
N+1 is the pattern where listing 100 employees fires 1 query for the list, then 100 more — one per employee — to fetch each one’s department. Each query is fast; the sum is 101 round-trips and a page that feels broken. The fix is to fetch in one round-trip: a JOIN in SQL, or an eager-load/batch API in an ORM.
The shape of the problem
1 query for the list + N queries for each item’s detail = N+1. Every individual query is fast; the pattern is quadratic-ish in round-trips.
Detect it
Symptom: a list page that slows as rows grow. Proof: query logging showing the same statement repeated with different ids.
Fix in SQL — one JOIN
Replace per-row lookups with a single JOIN that fetches list + details together in one round-trip.
Fix in ORMs — eager loading
JOIN FETCH (JPA), select_related (Django), includes (Sequelize), eager_load (Rails) — or one WHERE id IN (…) batch. Never query inside a loop.
Re-measure
101 queries becomes 1. The win was never the query cost — it was the 100 network round-trips and their latency.
1-- N+1: 1 + N queries2SELECT id, name, department FROM employees;3SELECT location FROM departments WHERE dept_id = 'Engineering';4SELECT location FROM departments WHERE dept_id = 'Sales';5-- … one per employee67-- Fixed: 1 query8SELECT e.name, d.location9FROM employees e10JOIN departments d ON e.department = d.dept_id;
| name | location |
|---|---|
| Alice | Berlin |
| Bob | Madrid |
| Cynthia | Berlin |
| David | Lisbon |
| Elena | Madrid |
| Farid | Berlin |
The loop version sends one extra query per employee — six employees, seven round-trips, and 10,000 employees would mean 10,001. The JOIN collects every pair in a single round-trip; the database does the lookup work where the data lives, in bulk, with one plan.
N+1 — the loop version
Innocent code, hidden storm.
1SELECT id, name, department FROM employees;2-- then, per employee row:3SELECT location FROM departments WHERE dept_id = 'Engineering';4SELECT location FROM departments WHERE dept_id = 'Sales';5-- … 1 + N round-trips
| result |
|---|
| 7 queries for 6 employees — 1 list + 6 lookups |
The JOIN fix — one round-trip
Ship the work to where the data lives.
1SELECT e.name, d.location2FROM employees e3JOIN departments d ON e.department = d.dept_id;
| name | location |
|---|---|
| Alice | Berlin |
| Bob | Madrid |
| Cynthia | Berlin |
| David | Lisbon |
| Elena | Madrid |
| Farid | Berlin |
The IN-batch fix — when JOINs don’t fit
Collect ids, fetch details in one statement.
1-- after the list query returns 6 department ids:2SELECT dept_id, location3FROM departments4WHERE dept_id IN ('Engineering', 'Sales', 'Marketing');5-- 2 queries total, then stitch in application code
| dept_id | location |
|---|---|
| Engineering | Berlin |
| Sales | Madrid |
| Marketing | Lisbon |
What it looks like in an ORM
Lazy loading is the usual culprit.
1# Django — bad:2for e in Employee.objects.all():3 print(e.department.location) # query per employee!45# Django — fixed:6for e in Employee.objects.select_related('department'):7 print(e.department.location) # 1 query total
| result |
|---|
| select_related → SQL JOIN under the hood — N+1 collapsed to 1 |
Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Take the queries you just visualized and see the data structures that power real databases.