The failure
A list page runs one query for its items. Code in the loop over those items then fetches each item’s related data with queries of its own: one item, one relation, one more query. Each of those queries is cheap. But every one is a round trip to the database, and the number of round trips grows with the number of items. With small dev data on a laptop next to the database, nobody notices.
The simulator
Start at 10 items on a laptop. Grow the list, switch to production, then fix it.
SimulatorLoad a list page
- DB queries
- 61
- Backend calls while rendering
- 20
Server queries 12 msCalls while rendering 9.2 ms
- SELECT * FROM items LIMIT 10
- SELECT … FROM related_a WHERE item_id = 1
- SELECT … FROM related_b WHERE item_id = 1
- SELECT … FROM related_c WHERE item_id = 1
- SELECT … FROM related_d WHERE item_id = 1
- SELECT … FROM related_a WHERE item_id = 2
- SELECT … FROM related_b WHERE item_id = 2
- SELECT … FROM related_c WHERE item_id = 2
… and 53 more
SELECT * FROM items LIMIT 10× 1SELECT … FROM related_a WHERE item_id = ?× 10SELECT … FROM related_b WHERE item_id = ?× 10SELECT … FROM related_c WHERE item_id = ?× 10SELECT … FROM related_d WHERE item_id = ?× 10SELECT … FROM related_e WHERE item_id = ?× 10 (while rendering, via a backend call)SELECT … FROM related_f WHERE item_id = ?× 10 (while rendering, via a backend call)
| Strategy | DB queries | Backend calls | Page time | Memory |
|---|---|---|---|---|
| Before: lazy loading (current) | 61 | 20 | 22 ms | 20 KB |
| Step 1: join in the base query | 21 | 20 | 9.6 ms | 21 KB |
| Step 2: load everything up front | 1 | 0 | 0.4 ms | 22 KB |
Model with illustrative numbers, not measurements from the real page.
What’s happening
Every mechanism here is general. None of it is a claim about one team’s code.
Lazy loading in a loop. The page sends one query for the list, then one query per relation per item. Page time is roughly the number of queries times the round trip. On a laptop the round trip is a fraction of a millisecond, so even thousands of queries hide. Some data is fetched only while the page renders, through another call to the backend, and each of those calls is a round trip of its own on top.
Before the fix, by the numbers below (about 5–7 queries per item):
| Items in the list | Queries for the items’ related data |
|---|---|
| 10 | 50–70 |
| 1,000 | 5,000–7,000 |
| 2,000 | 10,000–14,000 |
Fix step 1: join the related data into the base query. The loop over the items stops calling the database. One query returns the items with their related rows. The calls made while the page renders are still there.
Fix step 2: load everything up front. The parts of the front end that did not have the data they needed are restructured, so everything comes with the first load and nothing is fetched from the backend during rendering. The page sends one query.
The cost. Joined rows carry more data, so the page uses slightly more memory. In this case it was completely worth it for the speed gained.
Batching each relation into a single IN (…) query is another common fix.
A query budget is one way to automate the check: a test that loads the page and fails above a set number of queries. The simulator has one to try. This team relied on code review instead, as the next section says.
Where I hit this in production
The slow part was a page showing a list of items. Users reported it, and metrics confirmed it. It had not shown up earlier because the dev data was small.
The cause was lazy loading. Related data was fetched inside the loop over the items, and some of it only while the front-end page was rendering, which called the backend again. That came to about 5–7 queries per item, with up to 1,000–2,000 items in the list.
The fix came in two steps. First, more of the data was joined into the base query, so the loops over the items no longer called the database. Then the front-end parts that lacked that data were restructured, so everything was loaded up front instead of fetched from the backend during rendering. It cost slightly more memory.
To stop it coming back, the team has a code review procedure for it and puts emphasis on how queries are built. There is no automated check.
Why it matters more with AI
Without rules, AI is likely to generate N+1 code. So the team added rules about it to the agent rules used for writing code.