Skip to content
Back to skills

N Plus One

ASecurity

Find and fix N+1 query problems by eager-loading or batching the per-row queries

  • 3 stars
  • 0 votes
  • 0 copies
  • 2 views
  • Added September 3, 2026
ai-agentsgoperformance

Security analysis

A100/100

Scanned September 3, 2026

npx -y skills add black141312/ada --skill n-plus-one --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of N Plus One?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for N Plus One
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/black141312-n-plus-one/badge)](https://www.skillsdirectory.com/skills/black141312-n-plus-one)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: n-plus-one
description: Find and fix N+1 query problems by eager-loading or batching the per-row queries
category: performance
---

# N Plus One

Reach for this when an endpoint fires one query per row of a result set — the classic N+1 that scales linearly with data.

1. Spot the pattern: a query returns N rows, then a loop (or lazy relation access) issues one more query per row to fetch related data.
2. Confirm it in query logs or APM — look for the same statement repeated N times with only the id parameter changing.
3. Replace per-row loads with a single batched fetch: ORM eager loading (`includes`/`joinedload`/`prefetch_related`/`with`) or one `WHERE id IN (...)`.
4. For nested relations, eager-load the whole chain so child loops don't reintroduce N+1 at the next level.
5. Re-run and verify the query count collapses from N+1 to a small constant (1-3).
6. Add a test or dev-time assertion that fails on excessive query counts to prevent regressions.

## Rules
- Diagnose by query count, not wall time — N+1 hides on small datasets and explodes in production.
- Prefer the ORM's eager-load over hand-rolled loops; reach for `IN (...)` batching when the ORM can't.
- Watch serializers and view templates — lazily accessing a relation inside a render loop is a common hidden source.
- Don't fix N+1 by pulling the whole table into memory; batch by the ids you actually need.
- Cap or paginate the `IN (...)` list so a huge result set doesn't create one giant query.

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.