Search docs
Docs searchValidation rule
Back to Rule CatalogAP-DQR-005Data / Query RiskN+1 query evidence in collection loop
Flags collection workflows that appear to query inside a loop.
warningviolationNo auto-fix
How to fix
- Batch-load related records.
- Use include/select or joins where appropriate.
- Add tests or traces for collection-size behavior.
What the rule validates
A request path likely performs one query for each item in a collection.
Why it matters
N+1 queries become slow and expensive as collection size grows.
Common causes
- Related data is fetched per row.
- A loop calls a repository method repeatedly.
- Batch loading was not added for a new relationship.
Typical violation example
for each repository, query latest snapshot separately.Typical compliant example
Fetch latest snapshots for all repository ids in one bounded query.Related rules
AP-DQR-001 - Missing tenant filter evidence in multi-tenant SQLAP-DQR-002 - Broad SELECT * usage in query pathAP-DQR-003 - Unbounded query risk without limit/pagination evidenceAP-DQR-004 - Cross-module direct database access bypasses module boundary
Adjacent categories
Related files/config
apps/api/srcpackages/db/prisma/schema.prisma
Related CLI commands
archpilot validatearchpilot validate --ci