(Note · · 5 min read)

The list page runs four hundred queries

The slow page has no slow query in it. Every statement returns in a millisecond, the database reports nothing unusual, and the page still takes six seconds — because it is running one query per row, and the cost is in the count rather than in any one of them.

By Md. Habibur Rahman Shohel, full-stack & AI product engineer in Dhaka

The shape of the bug

Fetch a list, then for each item read something related — the author, the department, the current status. One query becomes one plus one per row. With ten rows nobody notices. With five hundred it is five hundred round trips, each carrying its own latency.

It is easy to write by accident because the code reads well. A loop over employees that touches each one's department looks like exactly what it is meant to do, and the queries are invisible at the call site.

Count queries, do not look for slow ones

The diagnosis is a count, not a duration. Log the number of statements per request, or watch the database's own statement log while loading the page. A page issuing four hundred queries is identified instantly by that number and never by a slow query report, where each one looks fine.

Make the count visible in development — printed per request, or asserted in a test for the endpoints that matter. A test that fails when a page's query count jumps catches the regression on the day it is introduced rather than when a customer's data grows.

Three fixes, in order of preference

  1. Load the related data in one additional query keyed by the identifiers you already have, then attach it in memory. Two queries regardless of row count, and the one most ORMs call eager loading.
  2. Join and select the few columns needed, when the relation is one-to-one and you want a single statement.
  3. Aggregate in the database — counts, sums, latest-per-group — rather than fetching rows to count them in the application.

What to avoid is caching the symptom. Putting a cache in front of four hundred queries leaves four hundred queries for anything the cache misses, and adds an invalidation problem to a solved problem.

Beware the nested case

Eager loading one level and then touching something two levels deep reintroduces the problem inside the loaded set. Load the whole shape the page needs, and let the page's needs rather than the model's structure decide what that shape is.

The reverse mistake is loading everything in case it is needed, which trades many small queries for one enormous result. The aim is the data the page uses, and nothing else.

Select the columns, not the row

On a wide table, fetching every column to display three is a substantial cost even in one query — more bytes over the wire, more memory, and index-only access lost. On a system with around two hundred tables, this is the difference between a report that returns and one that times out.

Then measure under load

Query count collapses fastest, but the result that matters is the one measured with concurrent users rather than one request at a time. On the payroll platform, work of this kind moved p95 response at 50 concurrent users from 1.9 seconds to 207 milliseconds, with CPU per request down 36% — a laptop benchmark on one API core with 10,000 seeded employees and 420,000 attendance records, measured 16 November 2026.

The CPU figure is the one to note. Fewer round trips does not only make a page faster, it makes the same hardware serve more people, which is the part a business feels.

Common questions

Why is my page slow when every query is fast?

Because the cost is in the number of queries, not their duration. Fetching a list and then reading something related per row makes one query become one per row, so five hundred rows means five hundred round trips that each look fine individually.

How do I find an N+1 query?

Count statements per request rather than looking for slow ones — log the count, or watch the database's statement log while loading the page. A slow query report shows nothing, because each statement is fast.

What is the best fix for N+1?

Load the related data in one extra query keyed by identifiers you already have and attach it in memory — two queries regardless of row count. Joins suit one-to-one relations, and aggregation belongs in the database. Caching in front of the problem leaves it intact on every miss.

Does fixing N+1 only help response time?

No. It cuts CPU per request, so the same hardware serves more people. On the payroll platform this work took p95 at 50 concurrent users from 1.9 s to 207 ms with 36% less CPU per request, in a laptop benchmark dated 16 November 2026.

Need something like this built?