Performance#

What pytest-queryguard costs when it is on, how that was measured, and what was done to lower it. The numbers come from one laptop and one afternoon; treat them as an order of magnitude and run the scripts on your own suite.

What the plugin does per statement#

With measurement on, every statement passes through a few listeners:

Once per test the collected statements are normalized and grouped by the detectors. The missing-index check, the index advisor and eager-load tracking are off by default and cost nothing until turned on; their costs are in the FAQ.

Part of the cost is not the plugin's own code. Registering any listener on these events makes SQLAlchemy take its slower event-dispatch path for every statement. In the benchmark below, four listeners that do nothing add about 6 microseconds per Core statement and about 8 per ORM statement, before any plugin code runs. That part cannot be removed without giving up the events.

Method#

Absolute times on this machine drifted by up to a factor of two between sessions (the same micro-benchmark gave 41 and 71 microseconds per Core statement with measurement off on different days), so only numbers measured back to back are compared below.

Micro-benchmark: in-memory SQLite#

Added time per statement with measurement on, median of 15 runs, two rounds each (both rounds shown):

Statement Measurement off Before: added After: added
Core select by key 41 us +10.5 us (+25 %), +11.3 us (+28 %) +8.8 us (+21 %), +9.1 us (+22 %)
ORM select by primary key 76 us +38.1 us (+50 %), +37.0 us (+48 %) +30.0 us (+39 %), +31.9 us (+42 %)

The added cost fell by about 17 % in both loops. The percentages are large because an in-memory SQLite statement is almost free. In the same session a no-op listener floor of about +6 us (Core) and +8 us (ORM) is included in every "added" figure.

Where the plugin's own part goes, per statement, measured by timing the listeners in place: about 4 us in the before_cursor_execute listener (call site, event, bookkeeping), 2 us in do_orm_execute and under 1 us in the timer listener. Those figures are from the 0.4.0 work. After 0.6.0 the plugin dropped its do_orm_execute listener: with any listener of that event SQLAlchemy prepares each ORM statement twice, which cost more than the listener itself. Strict mode and the unused eager-load check, both off by default, still add one, so a run with either pays that cost again.

A realistic case: a suite of 300 tests#

Backend Wall clock, off Wall clock, on Added
File-based SQLite (local disk) 38.0 to 39.0 ms/test 37.9 to 39.6 ms/test between -0.3 % and +2.2 %
PostgreSQL 16 (local container) 12.6 to 13.8 ms/test 12.6 to 13.2 ms/test between -9 % and 0 %

These are four independent runs of seven repetitions for each backend, two against the code before and two after the changes. The differences are smaller than the run-to-run noise: single runs on the same checkout differed by 10 % or more (fsync on a laptop disk, the database container), and the plugin often came out "faster" than the run without it. The honest reading is that on a suite that does real database work the overhead is below what this setup can resolve, in the order of a few percent at most.

The CPU time of the test process (which leaves out waiting for the disk and the server) is less noisy and gives a rough upper figure: between -10 % and +7 % of the suite's CPU time, which is the same conclusion with a wider error bar.

A profile of the same suite (cProfile, 300 tests) puts the time spent in the plugin's own functions at about 0.2 s in total, around 0.6 ms per test with the profiler's own inflation, and that figure fell by roughly 10 % with the changes below.

Measured again on 2026-11-10 (scripts/bench_suite.py --runs 5, the same laptop, its scratch directory no longer waiting on the disk), the same suite ran at about 5.4 ms a test without the plugin, wall clock and CPU time alike, and 0.8.0, 0.9.0, 0.10.0 and 1.0.0rc1 each added 18 to 20 % of the CPU time (about 1 ms a test). The head-to-head of the same day on a GitHub runner gave 1.0.0rc1 +13 to +15 % (see Comparison). So the "few percent" above holds where each test waits for the disk or the server; on a suite whose tests cost a few milliseconds of CPU, expect a seventh to a fifth.

Two limits of this measurement: the suite is small and made up for the benchmark, and the databases were on the same machine, so each statement was cheaper than it would be over a network. Over a network the plugin's share gets smaller, not larger.

What changed#

Behaviour is unchanged: the existing tests, the public API snapshot and the accuracy corpus give the same results, and each change has a regression test.

The call path (0.10.0)#

Since 0.10.0 the before_cursor_execute listener walks the stack from the statement up to the test function (the hook call that ran it), not only to the first frame of the application: the call path of every statement tells a loop from calls on separate lines, and the files on it tell the test code from the application (see How detection works). The call site comes from the same walk.

The walk from the first application frame to the test is the same for every pass of a loop, so its result is kept per measurement, looked up by the code object of each frame on that stretch and the instruction it is at: the passes after the first walk those frames without classifying them or computing their lines. The result depends on nothing else than the key and on answers kept per code object and per file for the whole session (is it a method of a Session subclass, is it test code), so the cache does not go stale; it is emptied when a measurement starts. Measured on the laptop above with Python 3.13.18, scripts/bench_overhead.py --runs 9, main (679e494) and the change interleaved, twice each:

Statement Measurement off before the walk (679e494): added the walk, first version 0.10.0: added
Core select by key 40 to 42 us +13.3 us, +13.5 us +19.6 us, +19.6 us +16.5 us, +15.7 us
ORM select by primary key 76 to 78 us +27.9 us, +26.6 us +43.2 us, +43.6 us +33.6 us, +36.3 us

1.0.0rc1 makes the walk cheaper again (SQLAlchemy's frames are passed with one lookup; see its changelog entry). On GitHub's runners, where the overhead gate measures, the ratio went down by 0.01 to 0.02 on some CPU models and stayed the same on others. On the laptop the difference is within the noise: measured on 2026-11-10 with Python 3.15.2, scripts/bench_overhead.py --runs 9, the tags v0.10.0 and v1.0.0rc1 interleaved. The laptop ran about 1.6 times slower after the first round (the "off" times); a round during which it changed speed is left out, and the two others are given separately:

Statement Round Measurement off 0.10.0: added 1.0.0rc1: added
Core select by key 1 39 us +16.2 us (41 %) +16.0 us (41 %)
Core select by key 2 65 to 66 us +26.6 us (40 %) +26.4 us (40 %)
ORM select by primary key 1 75 to 76 us +32.7 us (43 %) +31.9 us (42 %)
ORM select by primary key 2 127 us +54.3 us (43 %) +54.5 us (43 %)

The first version classified every application frame and computed its line for every statement (about 0.5 microseconds a frame); the walk of a deep stack (a request through a web framework's test client) still costs more than this loop at the top level of a script. A cache keyed by id() replaced the one keyed by the code object for the methods of a Session subclass: a code object hashes its bytecode and constants, which made the first version of the walk twice as slow.

What did not help#

How the overhead grew before this#

The relative ORM overhead in the micro-benchmark rose from about 35 % to about 50 % between the overhead gate's first commit and 0.4.0rc2. There is no single commit behind it: each feature that looks at every statement added one to three microseconds (column-load detection, the ORM flush tag, the Session scope, the autoflush attribution), and an in-memory SQLite statement is so cheap that each step shows as a few percentage points. Run to run the same commit varies by about 5 points, so a bisect needs repeated, pinned runs.

Comparison with other plugins#

A newer measurement, on a GitHub Actions runner with n-plus-one-sniffer too, is the head-to-head of 2026-11-10. The one below is from my machine.

pytest-queryguard 0.5.0, pytest-querycount 0.4.0 and queryspy 0.6.0, measured on 2026-11-05 with Python 3.14.4, SQLAlchemy 2.1.1 and pytest 9.1.1 on the machine above, by scripts/compare/run.py:

Each tool has its own virtual environment with the same Python, pytest and SQLAlchemy; every run is a fresh process pinned to one core, the tools' runs are interleaved and their order reversed every round. Two complete runs, one after the other (the cells show both when they differ):

pytest-queryguard pytest-querycount queryspy
300-test suite, added time +1.8 to +2.4 % +27 to +29 % +40 to +43 %
Core select, added per statement +25 to +27 us +108 to +112 us +13 us
ORM select, added per statement +55 to +66 us +184 to +187 us +172 to +176 us
Lazy load, added per statement +100 to +102 us +189 us +208 to +209 us

queryspy adds the least to a Core statement; on ORM statements, lazy loads and the whole suite pytest-queryguard adds the least. The absolute per-statement figures differ from the sections above because they are another session; as said in Method, only numbers measured back to back compare. A first, informal measurement on 2026-11-01 used a development build of pytest-queryguard and earlier versions of the other two.

Reproduce#

uv run python scripts/compare/run.py           # the comparison above
uv run python scripts/compare/run.py --queryguard .   # this checkout instead of 0.5.0
uv run python scripts/bench_overhead.py --statements 5000 --runs 15
uv run python scripts/bench_suite.py --runs 7
uv run python scripts/bench_suite.py --runs 7 --backend postgres \
    --url postgresql+pg8000://user:password@localhost:5432/bench

bench_suite.py --src measures another checkout, so two versions can be compared on the same machine; scripts/bench_gate.py is the CI check that guards the micro-benchmark ratio.

Edit this page on GitHub