Skip to content

The N+1 query that cost a client 2.4 seconds per page

The complaint was vague: the dashboard "feels heavy". Nobody had measured it, because everyone had learned to open it and go make tea.

Finding it

We enabled the query profiler on staging and loaded the page once. It ran 1,903 queries. Nineteen hundred of those were a single lookup inside a loop over line items, each one asking the database for a product name it had already fetched forty times.

The fix

One eager load and a keyed array. The page dropped to 14 queries and 310 milliseconds.

The lesson we keep relearning

Nobody wrote a slow query. Someone wrote a fast query in a loop, which is a different mistake and a much easier one to make. Query counts belong in your logs next to response times, because a count that triples overnight is a better alarm than a page that is merely a bit slower than last week.

Have a build in mind?

Send us the rough shape of it. You will get a written response with scope, a realistic timeline, and a fixed price band within one working day.