Skip to content

Code

Database query inside a loop (N+1 queries)code.query-in-loop

The code runs one database query per item in a loop. With 10 test rows it is instant; with 1,000 real rows it is 1,000 round trips, so pages time out, serverless functions hit their limit and the database connection pool runs dry.

  • In force Rule code.query-in-loop
  • 3 clauses

At a glance

code.query-in-loop
Severitymedium
CertaintyNeeds review — a heuristic; confirm before changing code
Looks atyour source code
When it runsRuns on every scan

How to fix it

Fetch everything in one query: `where: { id: { in: ids } }` (Prisma), `.in("id", ids)` (Supabase), `inArray(t.id, ids)` (Drizzle), or include the relation (`include: { author: true }`); write in bulk with createMany / one insert of many rows.

Check your own app

Run npx scanlyfix scan in your project. Every finding gets an id; after fixing, npx scanlyfix verify <id> proves it is gone.

If it is a false positive in your project, silence that one line with // scanlyfix-ignore-next-line code.query-in-loop -- <why>.

All rules

Your code stays
on your machine

Only findings are uploaded, and only to your own dashboard.