Search docs

Docs search
Back to Rule Catalog
AP-DQR-005Data / Query Risk

N+1 query evidence in collection loop

Flags collection workflows that appear to query inside a loop.

warningviolationNo auto-fix

How to fix

  1. Batch-load related records.
  2. Use include/select or joins where appropriate.
  3. Add tests or traces for collection-size behavior.

What it means

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.

Example bad pattern

for each repository, query latest snapshot separately.

Example good pattern

Fetch latest snapshots for all repository ids in one bounded query.

Related files/config

  • apps/api/src
  • packages/db/prisma/schema.prisma

Related CLI commands

  • archpilot validate
  • archpilot validate --ci
AP-DQR-005 - N+1 query evidence in collection loop | ArchPilot Docs