---
title: "Count the queries before you answer an N+1 review comment | Proofline"
description: "An N+1 claim is right when the page reads a relation once per row and the number of rows can grow."
url: https://proofline.sh/learn/ai-review-n-plus-one-comment/
---

[Learning center](/learn/)

# Count the queries before you answer an N+1 review comment

An N+1 claim is right when the page reads a relation once per row and the number of rows can grow. Count the queries the page runs with one row and with several. A flat count means you can decline the comment and quote the numbers. A count that grows with the rows means a code change, and the same comparison becomes the test.

Code review The Proofline team November 22, 2026 4 min read

## Read the claim as two checkable statements

An automated reviewer that reports an N+1 query is asserting two things. Something rendered per row, a template, a serializer field or a property, touches a related object. And the queryset that feeds that loop does not load the relation in advance. Django's [`select_related()`](https://docs.djangoproject.com/en/5.2/ref/models/querysets/#select-related) exists for the second part: it "results in a single more complex query but means later use of foreign-key relationships won't require database queries." [`prefetch_related()`](https://docs.djangoproject.com/en/5.2/ref/models/querysets/#prefetch-related) does the same for many-valued relations with one extra query per relation.

Either statement can be false. The loop may never touch the relation, or the queryset feeding it may already load what it needs. Each one takes a minute to check. A September 2026 pull request in Gyrinx, a Django application, contains both outcomes.

## A claim the author measured and declined

The pull request drew each user's badge beside their name on campaign screens. In its review, Copilot said a shared campaign row template "is also rendered by the signed-in dashboard", whose queryset "does not select the owner profile or prefetch `owner__badge_grants`", "so a page with several campaigns regresses to N+1." [Copilot review](https://github.com/gyrinx-app/gyrinx/pull/2556#pullrequestreview-5186802141)

The author answered: "declined, no change. The signed-in dashboard does not render `core/campaign/includes/campaign_row.html`". The dashboard draws its own row, "which draws the campaign's name, its status badge and a chevron, and never names the owner". They also measured it: "the dashboard costs 20 queries with one campaign listed and 20 with five (warm session, `CaptureQueriesContext`), so nothing grows with the number of rows." [Author's reply](https://github.com/gyrinx-app/gyrinx/pull/2556#issuecomment-5646628630)

The reply holds against the merged code. At the merge commit, the dashboard template `core/index.html` includes `core/includes/home/campaign_row.html`, and that file renders the campaign name, a status badge and a link, with no owner. The dashboard view also slices its unpinned campaigns with `[:5]`, so even a real per-row lookup there would cost at most five queries, plus one per pinned campaign. [Merged dashboard view](https://github.com/gyrinx-app/gyrinx/blob/7481049/n23/core/views/home.py) The reviewer had read the queryset correctly and assumed the wrong template.

## A claim a query-count test confirmed

A day later, Copilot flagged the view that adds a gang to a campaign. The view skipped loading the owner's badge data for every POST, but "can render the template after an invalid form or a refused POST", so those redraws "perform badge lookups during error handling (and can regress the page's query count)." [Copilot review](https://github.com/gyrinx-app/gyrinx/pull/2556#pullrequestreview-5189356039)

The author confirmed it: the redraws "lazy-loaded both — two queries more than the GET." They fixed it by always joining the owner's profile, and pinned the fix with a test that "posts a gang key the list did not offer and asserts the redraw's query count equals the GET's, and stays flat from one player to four." It "fails against the reviewed guard (23 vs 21) and passes with the join." [Author's reply](https://github.com/gyrinx-app/gyrinx/pull/2556#issuecomment-5650854457) The pull request merged on 13 September 2026 with the test:

```
address = reverse("n26-campaign-add-gang", args=[table.pk])
player(table, "vex")
on_get = self._queries(client, address)
with_one = redraw(address)
assert with_one == on_get
player(table, "kesh")
player(table, "ash")
player(table, "nyx")
with_more = redraw(address)
assert with_more == with_one
```

[Diff](https://github.com/gyrinx-app/gyrinx/pull/2556/files)

The test compares counts rather than pinning one. It asserts that four players cost what one costs, so an unrelated query added to the page later does not break it, while a per-row lookup does. Django's [`assertNumQueries`](https://docs.djangoproject.com/en/5.2/topics/testing/tools/#django.test.TransactionTestCase.assertNumQueries) pins an exact number instead, which is simpler to write and breaks more often.

## Decide from the counts

| What you find | Decision |
| --- | --- |
| The per-row code never reads the relation the comment names | Dismiss. Cite the template or serializer line |
| The relation is read per row, but code caps the rows at a small number, such as a slice or a fixed set of choices | Dismiss or defer. State the cap in your reply |
| The query count grows between one row and several | Change. Add `select_related()` or `prefetch_related()` and keep the two-size comparison as a test |
| You cannot run the page, but the query log shows the same statement repeated with different ids | Change, then add the test |
| The count is flat but high | This is not an N+1. Answer the comment and look at the count separately |

To get the counts, wrap the request in `django.test.utils.CaptureQueriesContext`, as both replies above did, or read the [`django.db.backends` logger](https://docs.djangoproject.com/en/5.2/ref/logging/#django-db-logger), which logs every application-level SQL statement at `DEBUG` level, but only when `settings.DEBUG` is `True`. Run it at two row counts with the same session state, because the first request on a cold session often pays for lookups that later requests skip.

## Size the rows before you weigh the fix

Two rows and five rows tell you whether the count grows. They do not tell you whether it matters. Use the largest row count the page can show in ordinary use. A list capped at five costs at most five extra queries. An organization's member list may hold hundreds. When the reviewer could not see the cap or the template, as in the dashboard case, the comment is a reasonable question, and the [dismissed finding guide](/learn/reconstruct-dismissed-review-finding/) shows how to record the answer so it is not counted as noise. If the bot raises the same claim on the next push, the measured reply is what you point to.

## One month of free code review.

New organizations get one calendar month of hosted code review for private and public GitHub repositories. No card or cloud connection required. [Eligibility and fair-use terms](/free-code-review/).

[Start free with GitHub](https://proofline.sh/orgs/plan?offer=free_month)

## Related reading

-   ### [A query with no limit: measure what it reads and how fast that grows](/learn/ai-review-unbounded-query-comment/)
    
    Code review The Proofline team November 22, 2026 4 min read
    
-   ### [Read a dismissed code review finding before you score it](/learn/reconstruct-dismissed-review-finding/)
    
    Code review The Proofline team November 20, 2026 4 min read
