Sphere
Log in Join
☆ 12

How do you spot N+1 queries before production?

Our ORM makes 300 queries on a page with a list of orders. Tests pass because they use a tiny dataset. What tooling do you use to catch this?

⚑ Report

3 replies

☆ 0
S Sofia ·2026-10-31#1

Log every SQL statement in test with a query counter and assert an upper bound per request. It is crude and very effective.

⚑ Report
☆ 0
M Mike ·2026-10-31#2

We use Django Debug Toolbar locally but it is not in CI.

⚑ Report
☆ 0
L Laura ·2026-11-01#3

Add a middleware that counts queries and fails the test if the count exceeds a threshold. Django has assertNumQueries, but a global ceiling catches more.

⚑ Report
Log in to reply.