N+1 Query
Database or fetch calls inside loops
warningPerformance
no-n-plus-oneWhy this matters
A database query inside a loop makes N+1 round trips to the database. For 100 items, that's 101 queries instead of 1. Response times grow linearly with data size.
Example
lib/posts.tsconst posts = await db.post.findMany();
for (const post of posts) {
// One query per post — N+1!
const author = await db.user.findUnique({
where: { id: post.authorId },
});
post.author = author;
}lib/posts.tsconst posts = await db.post.findMany({
include: { author: true },
});
// Single query with JOIN — no N+1How to fix
Use eager loading (include/join), batch queries (WHERE id IN), or DataLoader to fetch related data in a single query instead of per-item.
Reference
- In the output
- Findings carry
"ruleId": "no-n-plus-one"in--jsonand the same id in SARIF and in the terminal. - Silence one line
// prodlint-disable-next-line no-n-plus-one- Silence a whole file
// prodlint-disable no-n-plus-oneat the top of the file- How it works
- Reads the syntax tree, with a text fallback for files that don't parse. Source on GitHub