A test failed: now what#
A test that passed yesterday now fails with queryguard: N+1 detected. This
page is the checklist: read the report, decide whether the finding is real,
then fix it or leave it out on purpose. Nothing here needs a change to the
plugin's configuration unless you decide to silence something.
1. Read the report#
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))
- The second line says what repeated and how often: here one relationship of 50 different orders.
first atis the line of your code that sent the first of those statements: the first stack frame outside SQLAlchemy, pytest, the standard library andsite-packages. If it points to your test file instead of the application, the application is probably installed non-editable intosite-packages(see Limitations).called fromlines are the lines of your code that called it, innermost first, up to the test: the path through your helpers, with the frames of SQLAlchemy, pytest and the plugin left out. They are recorded the first time that line runs in the test.queryguard_stack_depthsets how many are shown (3 by default,0for none).parents loaded at(relationship loads only) is the line that ran the query whose rows became the parent objects: the place for the loader option, even when the query is built in a helper and the loop is somewhere else. Parents reached through other relationships (order.customer.country) name the query at the start of that chain, and thefixline chains the loaders:select(Order).options(joinedload(Order.customer).joinedload(Customer.country)). The line is left out when the plugin cannot tell, for example for objects the test created and the query handed back without reading them, or objects loaded before the test body started; the hint then names the relationship's own class. It is also left out when two queries from different places read the same objects, since the plugin cannot tell which result the loop uses. An object that a later query returns from the Session without reading its row again keeps the query that loaded it.kindandlabelname the finding the way ignore patterns, thequeryguard_ignoremarker and the baseline do:relationship-load Order.customeris the pattern for this one. For a repeated statement the label is the normalized statement, which takes the place of a separate line; a statement longer than 200 characters is cut and ends with*, so the label as printed still matches.docslinks to the section of How detection works that explains this kind of finding: what the rule counts, what it leaves out and why.sqlis one of the statements, as SQLAlchemy rendered it.fixis the usual remedy for this kind of finding. It is a hint, not a patch: check it against what the code needs.
The kind: line is what you need to silence a finding; the table below shows
how each headline maps to the kind and label, for the cases where you want to
write a pattern with a wildcard.
| The report says | Kind | Label (for ignore patterns and the baseline) | Usual fix |
|---|---|---|---|
Order.customer was lazy-loaded 50 times |
relationship-load |
Order.customer |
joinedload (many-to-one) or selectinload (collection) on the query that loaded the parents |
the same statement ran 7 times |
repeated-statement |
the normalized SQL, on the kind: line |
one query with WHERE id IN (...), or a join, instead of a loop |
Customer had its columns reloaded 50 times for 50 instances, after commit(), ... |
column-load |
Customer (expired) |
expire_on_commit=False, or load the objects again with one query |
Engineer loaded its subclass columns on its own 50 times ... |
column-load |
Engineer (polymorphic) |
selectin_polymorphic(Employee, [Engineer]) or with_polymorphic on the query, or polymorphic_load on the mapping |
Customer.name was loaded on its own 50 times for 50 instances (deferred column) |
column-load |
Customer.name |
undefer(Customer.name), or drop defer() / load_only() |
the same INSERT ran 6 times, one row at a time (opt-in) |
write-loop |
the normalized INSERT, UPDATE or DELETE |
session.add_all() and one flush, or session.execute(insert(Model), rows) |
Customer.notes and Customer.orders were joined into one query side by side, once (warns by default) |
cartesian-load |
Customer.notes + Customer.orders |
selectinload for all collections but one, see How detection works |
Customer.orders was loaded with selectinload and never read (experimental, off by default) |
unused-eager-load |
Customer.orders |
drop the loader option, see Unused eager loads |
Account: 12 columns loaded, 9 never read (experimental, off by default) |
unused-columns |
Account |
load_only() the columns the code reads, or defer() the large ones; see Unused columns |
len() of a Book queryset loaded all its rows (3 rows) only to count them (experimental, off by default, Django) |
queryset-len |
len(Book) or bool(Book) |
.count() for len(), .exists() for bool(); see Django |
queryguard: missing index (opt-in) |
missing-index |
the table | the CREATE INDEX on the fix: line; see the index advisor |
queryguard: expected at most 2 queries, got 4 |
a budget, not a finding | none | fix the code, or raise the budget; ignore patterns and the baseline do not apply |
To list the kind and label of every finding of a run in one file, write a scratch baseline:
pytest --queryguard-baseline=findings.json --queryguard-update-baseline
Then read findings.json and delete it. A budget test still fails this run,
since the baseline does not apply to budgets.
The SARIF file (--queryguard-sarif=PATH) carries the kind: line in each
message and the kind as the queryguard/<kind> rule id.
The same finding in many tests#
A loop in a shared helper or fixture is reported by every test that runs it, and each of them prints the same block. After the run, the summary names such a finding once:
========== queryguard: the same finding in several tests ==========
30 tests relationship-load Order.customer (900 times in total, from one place in the code; e.g. tests/test_orders.py::test_list[0])
a finding from one place in the code is fixed once for all its tests
A finding is listed when two tests or more reported it, failing or as a warning, grouped by kind and label. "From one place in the code" means every test reached it through the same function; with several places, the same relationship or statement is loaded in a loop in more than one function. The example is the first of the tests by name. Ignored and baselined findings are not counted, a test that runs again (a rerun plugin) counts once, and it works the same with pytest-xdist.
Strict mode: fail at the access#
The report comes when the test ends. With queryguard_strict = true (or
--queryguard-strict) the N+1 fails the test where it happens instead: the
lazy load of a relationship for the second parent loaded at the same line
(queryguard_relationship_threshold, 2 by default) raises
NPlusOneDetected, and the traceback ends at the line that read the
attribute, the frames of SQLAlchemy and the plugin left out:
def list_orders(session):
names = []
for order in _helper_query(session):
> name = order.customer.name
^^^^^^^^^^^^^^
E pytest_queryguard.NPlusOneDetected: queryguard strict: Order.customer was lazy-loaded for 2 parents loaded at the same line; this access is the N+1
E
E Order.customer was lazy-loaded 2 times
E first at app/orders.py:13 in list_orders
E called from tests/test_orders.py:5 in test_list_orders
E parents loaded at app/orders.py:7 in _helper_query
E kind: relationship-load label: Order.customer
E docs: https://maciejziel.github.io/pytest-queryguard/how-detection-works.html#relationship-load
E fix: load it with the parent query, e.g.
E select(Order).options(joinedload(Order.customer))
E
E (queryguard_strict raises at the lazy load instead of at the end of the
E test; a queryguard_ignore marker or pattern, an ignore() block or a
E baseline entry lets it pass, as for the report)
It is Rails' strict_loading!(mode: :n_plus_one_only) and Laravel's
preventLazyLoading() for SQLAlchemy:
- Reading the relationship of one object never raises, and neither do
objects whose query is unknown (made by the test, loaded before it). Objects
loaded one by one at the same line (
for id in ids: get_order(id).customer) count together, as in the report. The report at the end still covers the rest (there an object whose query is unknown counts with every query line), and withqueryguard_scope = sessioneach Session counts on its own here too. - The finding is the one the report makes, at the first lazy load, so
ignore patterns with
file=/function=and baseline entries match it. The analysis at the end still runs: SARIF,--queryguard-jsonand the summary include the N+1, and the exception replaces the report's text. - A query or database-time budget the test went over before the access
(
max_queriesormax_db_timemarker,queryguard_max_queries, the budget file, amax_queries()block) is shown under the N+1 in the exception, with the statements counted until the test stopped. A budget the rest of the test would have gone over is not known. - What the test would have run after the access is unknown, as for a test
that fails on its own: a
queryguard_ignorepattern of the test or anignore()block it entered is not listed under "stale ignores" for it. - It raises only what would fail the test anyway: in
failmode, for an N+1 that noqueryguard_ignoremarker or pattern or baseline entry covers. It does not raise while--queryguard-update-baselinerecords the findings. - Code that catches
AssertionErroraround the loop does not hide the N+1: the report at the end still fails the test. - A library that catches the exception and raises its own (Pydantic's
from_attributesvalidation, FastAPI'sresponse_model) fails the test with that error, the strict message usually inside it. The same goes for code that swallows the exception when the test then fails on its own. The analysis at the end still runs for such a test: SARIF,--queryguard-jsonand the summary include the N+1. When the error does not already carry the strict message, the report is shown in a "Captured queryguard call" section under the failure. The test's outcome stays that of its own error. --no-queryguard-strictturns strict mode off for one run, for example to see every N+1 of a test in the report instead of the first one.- It raises before the lazy load's
SELECTis prepared: nothing is flushed, no connection is used, and engine listeners such ashandle_errornever see the exception. For that, strict mode adds ado_orm_executelistener, which makes SQLAlchemy prepare each ORM statement twice: a strict run costs more per ORM statement than a default one. - Only lazy loads that ran their
SELECTcount towards the threshold, as in the report: a load that ado_orm_executelistener answers without one (a result cache), or that SQLAlchemy rejects before sending it, never counts and never raises. Lazy loads that several threads or asyncio tasks start at the same time count once theirSELECTs run, so the one that makes the N+1 may not raise; the report at the end still fails the test.
2. Is it real?#
Most findings are real: the code ran one statement per row, and the test database only hides the cost because it holds few rows. Look closer when:
- The test makes several requests, for example five calls through a test
client, and the repeated statement is an authentication or settings lookup.
The report says
across N different Sessions or connection checkoutsin that case. Nothing loops; setqueryguard_scope = session, see Several sessions or requests. - The loop is pagination or a batched
IN (...)load that runs five or more pages or chunks. The repeat rule counts each page; see Limitations. - Two parents each load a relationship once. That is reported by default, as the smallest N+1; the FAQ explains the threshold and how to raise it.
--queryguard-report prints how many statements each test ran, and
record_queries() lists every statement of a block with its
call site, if you want to look at them in the test itself.
3. Fix it#
Apply the hint at the line the report names and run the test again. The
fix: line shows the loader option, not its import: add joinedload or
selectinload to the file's from sqlalchemy.orm import line, or the hint
raises NameError. Two patterns cover most cases:
from sqlalchemy import select
from sqlalchemy.orm import joinedload, selectinload
# a many-to-one read for every row: join it into the parent query
orders = select(Order).options(joinedload(Order.customer))
# a collection read for every row: one extra query for all of them
customers = select(Customer).options(selectinload(Customer.orders))
# a session.get() or a SELECT in a loop: one query with IN
rows = session.scalars(select(Customer).where(Customer.id.in_(ids))).all()
A patch for the parent query#
Since 0.8.0; experimental (see Stability).
For a relationship-load finding, pytest-queryguard can write that change for
you. --queryguard-show-patch (or queryguard_show_patch = true) prints a
unified diff under the finding, and --queryguard-write-patch=PATH writes the diffs of the
whole run to one file:
pytest --queryguard-write-patch=queryguard.patch
git apply queryguard.patch # from the rootdir
--- a/app/orders.py
+++ b/app/orders.py
@@ -1,9 +1,9 @@
from sqlalchemy import select
-from sqlalchemy.orm import Session
+from sqlalchemy.orm import Session, joinedload
from app.models import Order
def list_orders(session: Session) -> list[Order]:
- orders = session.scalars(select(Order)).all()
+ orders = session.scalars(select(Order).options(joinedload(Order.customer))).all()
return [order.customer.name for order in orders]
The option goes on the select(Order) or session.query(Order) call of the
line in parents loaded at: as .options(...) right after the call, or as
one more argument of the .options(...) already in its chain. When that line
runs a statement built earlier in the function (statement = select(Order)
and then session.scalars(statement)), the option goes on the line that
builds it. A class named through its module (select(models.Order)) is
named the same way in the option: joinedload(models.Order.customer). The loader is
joinedload or selectinload, the same as in the fix: hint, chained
through the relationships that reached the parents. A loader the module does
not import yet is added to its from sqlalchemy.orm import line, or on a new
line after the imports at the top of the file.
A wrong patch is worse than none, so a finding gets one only when the line is
certain, and otherwise only the fix: hint:
- every parent was loaded by one query line, and the plugin knows the class it selected;
- that line is one statement with exactly one
select(...)call, or.query(...)on a session (see below), with that class as its only argument (Order, ormodels.Orderfor a module imported withimportorfrom ... import), and the statement runs nothing else. Asession.get(), a subquery, or a choice between queries (a if c else b,or, a comprehension) gets no patch; - that call is the statement the line runs, as it is: a
select(...), with only methods that keep its entities chained to it, is the first argument ofexecute,scalars,scalar,streamor SQLModel'sexecon a session; asession.query(...)with such methods is run by.all(),.first(),.one()or.one_or_none(), or iterated (for,list(...)). A select inside aunion_all(...), anexists(...), a subquery, a CTE, aninsert().from_select()or another statement, one passed to a helper (session.scalars(paginate(select(Order)))), turned into other columns (with_only_columns,with_entities,add_columns), a union or a textual statement (from_statement), or run by something else than a session (repository.scalars(select(Order))) gets no patch. A session is what is named so (session,self.session,db_session),db(db,self.db), or a parameter of the function annotated with a session type that the function does not rebind:conn: Session,s: orm.Session,conn: "AsyncSession",db: Annotated[Session, Depends(get_db)]; - or that line runs a variable on a session
(
session.execute(statement),session.scalars(statement)) that the function assigns once, on an earlier line, to such aselect(...)with only methods that keep its entities chained to it (where,join,order_by,limit,optionsand the like), and reads nowhere else. A variable that is reassigned or changed (statement = statement.where(...)), read again (passed to a count, for example), a parameter, a module-level name, assigned in a tuple or with:=, wrapped in a call (paginate(select(Order))), turned into a union or subquery, run by a helper (repository.scalars(statement)), or asession.query()kept in a variable gets no patch; selectis imported from SQLAlchemy or SQLModel, the classes (and the module inmodels.Order) are imported (or defined) in the module under their own names, the function does not rebind them, and an.options(...)already in the chain lists only SQLAlchemy loader calls that name each relationship asClass.attribute, none of them the relationship (not*options, an option a helper returns, or a relationship in a variable, a constant or a string:noload(REL),noload("customer"), which may be the one the patch loads, and two loaders of one relationship conflict). A chain through another class names that class on its own (.joinedload(Customer.country)), so the module must import it;- a
joinedload(a many-to-one) does not go on a statement withwith_for_update(),group_by(),having(),distinct()or a prefix or suffix: its outer join would putFOR UPDATEon the nullable side of the join, or the joined columns outside theGROUP BY. Aselectinloaddoes; - the file is inside the rootdir, UTF-8 with
\nor\r\nline ends (which the patch keeps), and was not modified after the session started, so it is the code that ran.
The diff paths are relative to the rootdir (a/app/orders.py). When the
rootdir is a subdirectory of the git repository, apply it with
git apply --directory=<subdirectory>, or with patch -p1 from the rootdir.
Fixes of the same file are merged into one diff; two relationships of one
query become two .options(...) calls, which SQLAlchemy accepts. Findings
left out by an ignore or the baseline get no patch. The patch is made when the
test's findings are reported, from its source file: no cost per statement.
Read it before you apply it, as any generated change: joinedload of a
collection or a large row can cost more than the lazy loads it replaces.
To keep the fix from regressing, put a budget on the test:
@pytest.mark.max_queries(2) fails it the day the count goes up again.
4. Or leave it out on purpose#
If the code is right as it is, or you cannot fix it now, leave the finding out, from the narrowest scope to the widest:
- one finding in one test:
@pytest.mark.queryguard_ignore("Order.customer", function="list_orders")(allow_nplusoneis the same marker under its old name); - one finding in the whole suite: a line in the
queryguard_ignoreoption, such asrelationship-load Order.customer function=list_orders; - everything that exists today: a baseline, so that only new findings fail;
- a whole kind as a warning only:
queryguard_fail_on; - every finding as a warning for one run:
pytest --queryguard-mode=warn.
Ignoring findings has the details. Ignore patterns that no longer match anything are listed as stale at the end of the run, so they do not outlive the code they were written for.
Next#
Once the suite is green, run it in CI (CI and GitHub Action); on a project with many existing findings, start from a baseline.