<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: maciej zielinski</title>
    <description>The latest articles on DEV Community by maciej zielinski (@maciejziel).</description>
    <link>https://dev.to/maciejziel</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4176146%2F96531993-43f0-4d21-8c25-83562eb7283d.png</url>
      <title>DEV Community: maciej zielinski</title>
      <link>https://dev.to/maciejziel</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/maciejziel"/>
    <language>en</language>
    <item>
      <title>My N+1 test plugin failed 551 tests on someone else's project. Nearly all of them were noise.</title>
      <dc:creator>maciej zielinski</dc:creator>
      <pubDate>Sun, 15 Nov 2026 21:58:13 +0000</pubDate>
      <link>https://dev.to/maciejziel/my-n1-test-plugin-failed-551-tests-on-someone-elses-project-nearly-all-of-them-were-noise-2pip</link>
      <guid>https://dev.to/maciejziel/my-n1-test-plugin-failed-551-tests-on-someone-elses-project-nearly-all-of-them-were-noise-2pip</guid>
      <description>&lt;p&gt;I maintain pytest-queryguard, a pytest plugin that fails a test when the code it runs sends one query per row. The problem it goes after is old: a test database holds a handful of rows, so a loop that lazy-loads a relationship costs a few extra queries and nobody notices. In production the same loop runs over hundreds of rows.&lt;/p&gt;

&lt;p&gt;This test passes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;test_list_orders&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;session&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;assert&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;list_orders&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;session&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;50&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and &lt;code&gt;list_orders&lt;/code&gt; looks fine in review:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;list_orders&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;session&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Session&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;list&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;dict&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;object&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;rows&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;order&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;session&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;scalars&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;select&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Order&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;all&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;rows&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"customer"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;customer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;rows&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With the plugin installed, the same test fails:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;queryguard: N+1 detected

  Order.customer was lazy-loaded 50 times
  first at app/orders.py:10 in list_orders
    called from tests/test_orders.py:7 in test_list_orders
  parents loaded at app/orders.py:9 in list_orders
  kind: relationship-load  label: Order.customer
  docs: https://maciejziel.github.io/pytest-queryguard/how-detection-works.html#relationship-load
  sql: SELECT customer.id, customer.name FROM customer WHERE customer.id = ?
  fix: load it with the parent query, e.g.
       select(Order).options(joinedload(Order.customer))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Nothing to import or configure. Add the &lt;code&gt;joinedload&lt;/code&gt; and it passes.&lt;/p&gt;

&lt;h2&gt;
  
  
  Then I ran it on a suite I didn't write
&lt;/h2&gt;

&lt;p&gt;My own corpus of scenarios said the rules were accurate. Then I ran the plugin on &lt;a href="https://github.com/infobyte/faraday" rel="noopener noreferrer"&gt;Faraday&lt;/a&gt;, a Flask-SQLAlchemy app with 2112 tests on PostgreSQL. With the defaults of the code I had at the time, 551 tests failed.&lt;/p&gt;

&lt;p&gt;I went through them, and nearly all were noise. The biggest group, 422 findings, came from Faraday's user factory: its &lt;code&gt;post_generation&lt;/code&gt; hook looks up the role for every user it creates. That is an N+1, technically. It also never runs in production, and fixing it changes nothing for anyone using Faraday.&lt;/p&gt;

&lt;p&gt;About 480 more came from a statistics helper called on six separate lines of a test. The plugin saw the same counts repeat and called it a loop. The rest were mostly tests sending several requests through one &lt;code&gt;Session&lt;/code&gt; that the whole suite shares.&lt;/p&gt;

&lt;p&gt;A tool that fails a quarter of your suite on the first run gets uninstalled on the first run. So 0.10.0 changed two things.&lt;/p&gt;

&lt;h2&gt;
  
  
  Test code is its own category now
&lt;/h2&gt;

&lt;p&gt;If every frame of your code on a statement's stack is test code (the test module, &lt;code&gt;conftest.py&lt;/code&gt;, files under &lt;code&gt;tests/&lt;/code&gt;, any module that defines a factory_boy or polyfactory factory), its N+1 no longer fails the test. It is counted, and the run ends with a section like this (the example from the docs):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;============================ queryguard: test code =============================
422 N+1 findings in test code left out (299 tests): the statements ran only in test modules, conftest.py, factories or helpers under tests/, not in the application.
To see them: pytest -o queryguard_test_code=warn, or set queryguard_test_code = warn (or fail) in the pytest configuration.
Why: https://maciejziel.github.io/pytest-queryguard/how-detection-works.html#test-code
  tests/test_accounts.py::test_create_account
  tests/test_accounts.py::test_delete_account
  tests/test_billing.py::test_invoice_lines
  tests/test_billing.py::test_refund
  tests/test_orders.py::test_list_orders
  and 294 more
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;One frame of the application on the stack is enough to make the statement the application's, so a loop in your code that a test helper calls is still reported. The rule is by file, which means a function written inside a test module is test code too. If that's how you reproduce bugs, set &lt;code&gt;queryguard_test_code = fail&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The second change: every statement now carries its call path, the lines of your code from the statement up to the test. Two calls on separate lines have different paths and count apart; a loop has one path. That stack walk isn't free. In 0.10.0, on my in-memory SQLite micro-benchmark, it added about 3 µs per Core statement and 8 per ORM statement. On the 300-test suite I use for comparisons, where a test costs about 5 ms, 1.0.0 adds 13 to 15% on a GitHub runner and 18 to 20% on my laptop. On a suite where each test waits for a real database it's a few percent.&lt;/p&gt;

&lt;p&gt;On Faraday, failures went from 551 to 79. Some of those 79 are real N+1s in its serializers. The others are loops in the test that call the application once per pass, ten paginated requests in a &lt;code&gt;for&lt;/code&gt; loop for example. From the stack, that looks exactly like a loop in the application, and I don't have a way to tell them apart.&lt;/p&gt;

&lt;h2&gt;
  
  
  Django, and how experimental it is
&lt;/h2&gt;

&lt;p&gt;0.10.0 also added Django support. It runs next to pytest-django, with nothing in &lt;code&gt;INSTALLED_APPS&lt;/code&gt; or settings, on Django 5.2, 6.0 and 6.1. The fix in the message names &lt;code&gt;select_related()&lt;/code&gt; or &lt;code&gt;prefetch_related()&lt;/code&gt;, and on 6.1 &lt;code&gt;.fetch_mode(models.FETCH_PEERS)&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;It is marked experimental: findings and options can change in a minor release. It doesn't have the unused-eager-load, unused-columns or cartesian-load checks, the static check, or the in-app mode yet. That is still the case in 1.0.0: the stable API covers the SQLAlchemy side, and Django stays experimental, along with the static check and the MCP server. Taking Django out of experimental is the plan for 1.1.&lt;/p&gt;

&lt;p&gt;To show what "experimental" means in practice: the first 1.0 release candidate fixed a bug where a DRF &lt;code&gt;ListAPIView&lt;/code&gt; with a class-level &lt;code&gt;queryset&lt;/code&gt; and a nested serializer passed silently. That view runs no line of your project, so the plugin took its statements for the test's own code and left them out. The most common DRF N+1, missed by default, by the rule I just described.&lt;/p&gt;

&lt;p&gt;On six Django projects (DRF, Read the Docs, django-cms, pretix, saleor, django-oscar; 8,945 tests), the plugin in warn mode changed no test outcome. I read 98 of its findings in the projects' code: 68 were real loops, 28 debatable, 2 false. That run was on 2026-11-07, two days before the release candidate.&lt;/p&gt;

&lt;h2&gt;
  
  
  The other tools
&lt;/h2&gt;

&lt;p&gt;If you're on Django and django-zeal works for you, keep it. It has covered Django for years and it's the established choice. On my Django corpus of 73 scenarios, pytest-queryguard had 100% precision and 89% recall by default, django-zeal 2.3.0 93% and 70%, nplusone 84% and 43%. I wrote that corpus together with the integration, so 100% there means the cases I thought of work, not that it's right on your code. On overhead they're close: on a 300-test pytest-django suite mine added 3.0% at the median, django-zeal −0.5%, which is noise.&lt;/p&gt;

&lt;p&gt;nplusone had its last release in 2018. It patches &lt;code&gt;Query.__iter__&lt;/code&gt;, so on SQLAlchemy 2.0 it misses parents loaded with &lt;code&gt;select()&lt;/code&gt;, and on 2.1 the import fails. My corpus has no lazy-load scenario written with &lt;code&gt;session.query()&lt;/code&gt;, the API it patches, so I can't tell you how it does on legacy code.&lt;/p&gt;

&lt;p&gt;For SQLAlchemy I ran every pytest tool I found on 278 scenarios in GitHub Actions, on 2026-11-10, with the release candidate (1.0.0 has the same code). With defaults, pytest-queryguard flagged 117 of 135 buggy scenarios and 2 of 143 clean ones. queryspy, pytest-querycount and n-plus-one-sniffer flagged 110 to 115 buggy and 30 to 35 clean. Discount that heavily. I wrote the corpus, its clean side includes the pagination and chunking shapes my rules were built to skip, and three clean scenarios use my own &lt;code&gt;ignore()&lt;/code&gt; API. The others catch nearly every N+1 shape they claim to; pytest-querycount caught all of them. They also report runs of 2 to 4 identical statements, which mine doesn't by default. The workflow is public; fork it and run &lt;code&gt;gh workflow run head-to-head.yml&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  In CI
&lt;/h2&gt;

&lt;p&gt;Any CI that runs pytest already enforces it. There is also a GitHub Action that uploads the findings as SARIF and, on a pull request, compares query counts with the base branch:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="na"&gt;uses&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;MaciejZiel/pytest-queryguard-action@v1&lt;/span&gt;
  &lt;span class="na"&gt;with&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;pytest-args&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;tests/&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The comment it leaves looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight markdown"&gt;&lt;code&gt;&lt;span class="gu"&gt;### pytest-queryguard: queries compared with the base branch
&lt;/span&gt;
412 tests compared: 3120 -&amp;gt; 3187 statements (+67). 2 run more, 1 fewer; 1 new finding, 0 fixed.

&lt;span class="gu"&gt;#### New findings
&lt;/span&gt;| Kind | Label | Where | Times | Tests |
| relationship-load | &lt;span class="sb"&gt;`Order.items`&lt;/span&gt; | &lt;span class="sb"&gt;`app/orders.py in list_orders`&lt;/span&gt; | 50 | &lt;span class="sb"&gt;`tests/test_orders.py::test_list`&lt;/span&gt; |
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A reviewer sees that a change made a test run more queries even when no rule fired yet.&lt;/p&gt;

&lt;h2&gt;
  
  
  What it doesn't see
&lt;/h2&gt;

&lt;p&gt;Statements that skip SQLAlchemy and Django's database layer: a raw &lt;code&gt;sqlite3&lt;/code&gt; or &lt;code&gt;asyncpg&lt;/code&gt; connection, Peewee, any other ORM. It doesn't say so either. It only measures the test body, not fixtures. Work spread over threads counts together. And a finding says a pattern happened, not that production is slow. The &lt;a href="https://maciejziel.github.io/pytest-queryguard/limitations.html" rel="noopener noreferrer"&gt;limitations page&lt;/a&gt; has the rest.&lt;/p&gt;

&lt;h2&gt;
  
  
  Trying it
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pip install pytest-queryguard
pytest
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For Django: &lt;code&gt;pip install "pytest-queryguard[django]" pytest-django&lt;/code&gt;. On an existing suite, record a baseline once and only new findings fail:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pytest --queryguard-baseline=queryguard-baseline.json --queryguard-update-baseline
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The repo is &lt;a href="https://github.com/MaciejZiel/pytest-queryguard" rel="noopener noreferrer"&gt;github.com/MaciejZiel/pytest-queryguard&lt;/a&gt;. If one of my scenarios is labelled wrong, or I configured your tool badly in the comparison, open an issue. I'd rather fix the benchmark than defend it.&lt;/p&gt;

</description>
      <category>python</category>
      <category>testing</category>
      <category>fastapi</category>
      <category>django</category>
    </item>
    <item>
      <title>Why your tests don't catch N+1 queries (and how to make them)</title>
      <dc:creator>maciej zielinski</dc:creator>
      <pubDate>Sun, 11 Oct 2026 15:27:57 +0000</pubDate>
      <link>https://dev.to/maciejziel/why-your-tests-dont-catch-n1-queries-and-how-to-make-them-gdg</link>
      <guid>https://dev.to/maciejziel/why-your-tests-dont-catch-n1-queries-and-how-to-make-them-gdg</guid>
      <description>&lt;p&gt;N+1 queries are easy to merge with a green CI run. The tests covered the endpoint and checked the response. They did not check how many queries it took to produce that response, so nothing failed. This post covers why the usual test setup hides N+1 queries, how to make a test fail on them, and a related problem the same setup hides: queries that have no index to use.&lt;/p&gt;

&lt;p&gt;The examples use SQLAlchemy 2 and pytest. The tool at the end is pytest-queryguard, a plugin I wrote; the techniques before it work without it.&lt;/p&gt;

&lt;h2&gt;
  
  
  The bug
&lt;/h2&gt;

&lt;p&gt;Two models, a blog post and its author:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Author&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Base&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;__tablename__&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;author&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;

    &lt;span class="nb"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Mapped&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;mapped_column&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;primary_key&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Mapped&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;


&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Base&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;__tablename__&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;post&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;

    &lt;span class="nb"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Mapped&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;mapped_column&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;primary_key&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Mapped&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="n"&gt;author_id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Mapped&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;mapped_column&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;ForeignKey&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;author.id&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="n"&gt;author&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Mapped&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;Author&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;relationship&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And a function that builds a feed:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;feed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;session&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Session&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;list&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;dict&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;]]:&lt;/span&gt;
    &lt;span class="n"&gt;posts&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;session&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;scalars&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;select&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Post&lt;/span&gt;&lt;span class="p"&gt;)).&lt;/span&gt;&lt;span class="nf"&gt;all&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;[{&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;title&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;author&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;author&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;posts&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;select(Post)&lt;/code&gt; loads the posts. &lt;code&gt;p.author&lt;/code&gt; is a lazy relationship, so the first time the comprehension touches it for a given author, SQLAlchemy sends &lt;code&gt;SELECT ... FROM author WHERE author.id = ?&lt;/code&gt;. One query for the list, then one more for each author it has not seen yet. That is the N+1.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why the test passes
&lt;/h2&gt;

&lt;p&gt;The fixture creates 3 authors and 6 posts, and the test checks the length:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;test_feed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;session&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;assert&lt;/span&gt; &lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;feed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;session&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It passes. It would pass in production too: the function returns the right data. The difference is cost. With 3 authors the function sends 4 queries and finishes in microseconds against SQLite. With 500 posts by 400 authors on a real database over a network, it sends 401 round trips per request.&lt;/p&gt;

&lt;p&gt;Three things in a normal test setup hide this:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;The data is tiny.&lt;/strong&gt; A test database holds a handful of rows on purpose, so the extra queries cost nothing measurable. No test times out and nobody notices.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Assertions check results, not work.&lt;/strong&gt; &lt;code&gt;len(...) == 6&lt;/code&gt; is true whether it took 1 query or 400.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The identity map can hide the queries completely.&lt;/strong&gt; This one surprised me while preparing the example. If the test reuses the session that inserted the rows, the authors are already in that session's identity map, so &lt;code&gt;p.author&lt;/code&gt; is answered from memory and no lazy-load SQL is sent at all. I ran the same lazy &lt;code&gt;feed&lt;/code&gt; code in a test that inserts and reads in one session, with the plugin installed and &lt;code&gt;--queryguard-report&lt;/code&gt;:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   variant_identity_map/test_identity_map.py .                              [100%]

   ============================== queryguard report ===============================
      17  variant_identity_map/test_identity_map.py::test_feed_same_session
   ============================== 1 passed in 0.02s ===============================
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The 17 statements are the table setup, the inserts and a single &lt;code&gt;SELECT&lt;/code&gt; of the posts. The bug is in the code, but the database never sees it, so nothing watching the SQL can see it either. The fix is in the fixture: insert with one session, close it, and give the code under test a fresh one. The rest of this post uses a fixture that does that.&lt;/p&gt;

&lt;h2&gt;
  
  
  Making the test fail
&lt;/h2&gt;

&lt;p&gt;The usual answer, which you will find on Stack Overflow going back to 2013, is to listen to SQLAlchemy's &lt;code&gt;before_cursor_execute&lt;/code&gt; event and count statements:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;sqlalchemy&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;event&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;sqlalchemy.engine&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Engine&lt;/span&gt;

&lt;span class="n"&gt;statements&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
&lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;listen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Engine&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;before_cursor_execute&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
             &lt;span class="k"&gt;lambda&lt;/span&gt; &lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;cursor&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;stmt&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;statements&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stmt&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then assert the count. That works, and if a counter is all you need, those few lines are enough. When I tried this in my own projects, I kept wanting three more things: a check on every test without writing a number for each one; a message that says which relationship and which line caused the repeats; and something that understands async sessions and FastAPI's &lt;code&gt;TestClient&lt;/code&gt;, which runs your app in another thread.&lt;/p&gt;

&lt;p&gt;pytest-queryguard does that. Install it and run the same, unchanged test:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pip &lt;span class="nb"&gt;install &lt;/span&gt;pytest-queryguard
pytest
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tests/test_feed.py F                                                     [100%]

=================================== FAILURES ===================================
__________________________________ test_feed ___________________________________
queryguard: N+1 detected

  Post.author was lazy-loaded 3 times
  first at app/feed.py:9 in feed
  sql: SELECT author.id, author.name FROM author WHERE author.id = ?
  fix: load it with the parent query, e.g.
       select(Post).options(selectinload(Post.author))
=========================== short test summary info ============================
FAILED tests/test_feed.py::test_feed - Failed: queryguard: N+1 detected
============================== 1 failed in 0.02s ===============================
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There is no configuration and no marker. The plugin listens to two SQLAlchemy events for the duration of each test function (fixture setup is not counted): &lt;code&gt;do_orm_execute&lt;/code&gt; on &lt;code&gt;Session&lt;/code&gt;, which reports lazy loads of a relationship, and &lt;code&gt;before_cursor_execute&lt;/code&gt; on &lt;code&gt;Engine&lt;/code&gt;, which sees every statement. A test fails when one relationship is lazy-loaded for 2 or more parent objects, or when one &lt;code&gt;SELECT&lt;/code&gt;, with its parameters removed, runs 5 or more times. Both thresholds are settings.&lt;/p&gt;

&lt;p&gt;The relationship rule is the useful one here. It fires with 3 authors, on test data, because it does not need the repetition to be large; it only needs the pattern.&lt;/p&gt;

&lt;h2&gt;
  
  
  The fix
&lt;/h2&gt;

&lt;p&gt;The message already suggests it. Load the authors together with the posts:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;feed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;session&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Session&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;list&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;dict&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;]]:&lt;/span&gt;
    &lt;span class="n"&gt;stmt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;select&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Post&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;options&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;selectinload&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Post&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;author&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="n"&gt;posts&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;session&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;scalars&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stmt&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;all&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;[{&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;title&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;author&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;author&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;posts&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;selectinload&lt;/code&gt; sends a second query, &lt;code&gt;SELECT ... FROM author WHERE author.id IN (...)&lt;/code&gt;, for all the authors at once. Two queries, whether there are 6 posts or 6000. &lt;code&gt;joinedload&lt;/code&gt; is the other common choice and works well for many-to-one relationships like this one; &lt;code&gt;selectinload&lt;/code&gt; is usually the safer default for collections.&lt;/p&gt;

&lt;h2&gt;
  
  
  Query budgets
&lt;/h2&gt;

&lt;p&gt;The N+1 rules catch loops. They do not catch a page that slowly grows from 2 queries to 7 because someone added a count, a permission check and another relationship. For pages where you care about the number, write it down:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="nd"&gt;@pytest.mark.max_queries&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;test_post_page&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;session&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;assert&lt;/span&gt; &lt;span class="nf"&gt;post_page&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;session&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;author_posts&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Against this version of &lt;code&gt;post_page&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;post_page&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;session&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Session&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;post_id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;dict&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;object&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt;
    &lt;span class="n"&gt;post&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;session&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Post&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;post_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;author&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;post&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;author&lt;/span&gt;
    &lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;session&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;scalar&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="nf"&gt;select&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;func&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;count&lt;/span&gt;&lt;span class="p"&gt;()).&lt;/span&gt;&lt;span class="nf"&gt;select_from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Post&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;where&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Post&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;author_id&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;author&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;title&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;post&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;author&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;author&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;author_posts&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;pytest --queryguard-report&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tests/test_feed.py .F                                                    [100%]

=================================== FAILURES ===================================
________________________________ test_post_page ________________________________
queryguard: expected at most 2 queries, got 3

  1. SELECT post.id, post.title, post.author_id FROM post WHERE post.id = ?  (app/feed.py:14 in post_page)
  2. SELECT author.id, author.name FROM author WHERE author.id = ?  (app/feed.py:15 in post_page)
  3. SELECT count(*) AS count_1 FROM post WHERE post.author_id = ?  (app/feed.py:16 in post_page)
============================== queryguard report ===============================
    3  tests/test_feed.py::test_post_page
    2  tests/test_feed.py::test_feed
=========================== short test summary info ============================
FAILED tests/test_feed.py::test_post_page - Failed: queryguard: expected at m...
========================= 1 failed, 1 passed in 0.03s ==========================
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The failure lists every statement with the line that sent it, and the report at the bottom ranks tests by query count, which is a quick way to find the heaviest tests in a suite you did not write. Loading the author with the post, &lt;code&gt;session.get(Post, post_id, options=[joinedload(Post.author)])&lt;/code&gt;, brings it to 2 and the test passes. There is also a &lt;code&gt;max_queries()&lt;/code&gt; context manager for budgeting one block of code instead of a whole test.&lt;/p&gt;

&lt;h2&gt;
  
  
  The other thing tiny databases hide: missing indexes
&lt;/h2&gt;

&lt;p&gt;That count query has a second problem that no amount of query counting will find. There is no index on &lt;code&gt;post.author_id&lt;/code&gt;. With 6 rows, PostgreSQL reads the whole table and nobody can tell; with 10 million rows, the same query reads 10 million rows.&lt;/p&gt;

&lt;p&gt;You cannot catch this by looking at &lt;code&gt;EXPLAIN&lt;/code&gt; in a test, because on a tiny table a sequential scan is the planner's correct choice even when an index exists. The trick is to ask a different question: plan the query again with &lt;code&gt;enable_seqscan&lt;/code&gt; switched off. If PostgreSQL still reads the whole table to apply the filter, then no index can serve that filter, however small the table.&lt;/p&gt;

&lt;p&gt;pytest-queryguard 0.2 does this when you ask for it (PostgreSQL 16 or newer, a sync driver; this run used pg8000 against PostgreSQL 17 in Docker):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pytest &lt;span class="nt"&gt;--queryguard-index-check&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tests/test_feed.py .F                                                    [100%]

=================================== FAILURES ===================================
________________________________ test_post_page ________________________________
queryguard: missing index

  post is read in full: Seq Scan with filter (post.author_id = $1)
  first at app/feed.py:16 in post_page
  sql: SELECT count(*) AS count_1 FROM post WHERE post.author_id = %s::INTEGER
  fix: CREATE INDEX ON post (author_id)

  a sequential scan is fine on a small lookup table:
  list it in queryguard_index_ignore_tables
=========================== short test summary info ============================
FAILED tests/test_feed.py::test_post_page - Failed: queryguard: missing index
========================= 1 failed, 1 passed in 0.15s ==========================
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note that &lt;code&gt;test_feed&lt;/code&gt; passed: its &lt;code&gt;IN&lt;/code&gt; query filters on the author's primary key. After adding &lt;code&gt;index=True&lt;/code&gt; to &lt;code&gt;Post.author_id&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tests/test_feed.py ..                                                    [100%]

============================== 2 passed in 0.17s ===============================
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each distinct &lt;code&gt;SELECT&lt;/code&gt; is explained once per test, inside a savepoint on the connection that ran it, so it sees the test's uncommitted rows and leaves the transaction as it was. On SQLite the check does not fail anything; it prints one line at the end of the session saying the statements were not checked.&lt;/p&gt;

&lt;p&gt;PostgreSQL 16 matters because of &lt;code&gt;EXPLAIN (GENERIC_PLAN)&lt;/code&gt;, which plans a statement with &lt;code&gt;$1&lt;/code&gt; placeholders without values. Before 16 there is no clean way to do this from the SQL your ORM actually sent.&lt;/p&gt;

&lt;h2&gt;
  
  
  Limitations
&lt;/h2&gt;

&lt;p&gt;Worth knowing before you adopt any of this:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;SQLAlchemy only.&lt;/strong&gt; Raw DB-API calls and other ORMs are invisible to the plugin. For Django, use &lt;code&gt;assertNumQueries&lt;/code&gt; or a Django-specific tool.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;It only sees SQL that is sent.&lt;/strong&gt; The identity-map case above passes. So does code that is never executed by a test.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Writes are never reported as N+1.&lt;/strong&gt; At cursor level a unit-of-work flush looks the same as an application write loop, so &lt;code&gt;INSERT&lt;/code&gt;/&lt;code&gt;UPDATE&lt;/code&gt;/&lt;code&gt;DELETE&lt;/code&gt; are left to budgets.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;No unused-eager-load detection.&lt;/strong&gt; nplusone does this; pytest-queryguard does not.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The index check needs PostgreSQL 16+ and a sync engine.&lt;/strong&gt; Async drivers prepare every statement, and PostgreSQL rejects &lt;code&gt;EXPLAIN (GENERIC_PLAN)&lt;/code&gt; sent that way, so async statements are skipped with a notice. It judges plans, not data: it tells you no index can serve a filter, not that an existing index is good. Joins without a &lt;code&gt;WHERE&lt;/code&gt; on the unindexed side are not reported, because that plan depends on statistics.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;It costs time.&lt;/strong&gt; In the project's own measurement, the index check added about 2–5 ms per distinct statement against a local Docker PostgreSQL. N+1 detection alone is cheap.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Small lookup tables&lt;/strong&gt; are fine to scan in production too. List them in &lt;code&gt;queryguard_index_ignore_tables&lt;/code&gt; or mark a test with &lt;code&gt;@pytest.mark.allow_missing_index&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What about nplusone?
&lt;/h2&gt;

&lt;p&gt;nplusone did this job for years and still gets a lot of downloads. Its last release was 1.0.0 in 2018. It patches &lt;code&gt;Query&lt;/code&gt; internals that SQLAlchemy 1.4 rewrote, so on 1.4 and 2.0 it does not detect lazy loads and does not tell you it stopped working (see nplusone issue #37). If you have it in a SQLAlchemy 2 project, check that it still fails on a known N+1 before relying on it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Use a fresh session for the code under test, or the identity map hides lazy loads.&lt;/li&gt;
&lt;li&gt;Make tests fail on the N+1 pattern, not on slowness; test data is too small to be slow.&lt;/li&gt;
&lt;li&gt;Put a query budget on the endpoints you care about.&lt;/li&gt;
&lt;li&gt;On PostgreSQL, plan your test queries with sequential scans disabled to find missing indexes while the tables are still small.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;pytest-queryguard is MIT-licensed and on PyPI. Source, full documentation and the issue tracker: &lt;a href="https://github.com/MaciejZiel/pytest-queryguard" rel="noopener noreferrer"&gt;https://github.com/MaciejZiel/pytest-queryguard&lt;/a&gt;. Bug reports and "it missed this N+1" examples are the most useful thing you can send.&lt;/p&gt;

</description>
      <category>database</category>
      <category>performance</category>
      <category>python</category>
      <category>testing</category>
    </item>
  </channel>
</rss>
