orm-query-review · git:20261120.76a1b83 · 2026-11-20 · sha256 250cbd938ba22fcb

orm-query-review git:20261120.76a1b83A

Immutable. This exact content never changes and is served at /api/v1/blob/250cbd938ba22fcb.

No tool to install first
claude plugin marketplace add https://markdownregistry.com/mp/a/art_omsfl3enrtsqypsj/250cbd938ba22fcbe5d64553a27a048343b2741aee388c8d9f33ea678ec45740.json

Then /plugin install orm-query-review@markdownregistry-omsfl3enrtsqypsj in Claude Code. It installs this exact version, the one graded A here, pinned to the commit it was graded at, and leaves it there.

---
name: orm-query-review
description: "Reviews ORM code for N+1 queries, lazy loading, over-fetching and missing transactions, and proves each finding by counting queries."
---

# ORM query review

Show the SQL the ORM really sends. Judge the code by statements per request, not by how it reads.

## Steps

1. Turn on query logging for the framework in use (Django `connection.queries`, Rails log, Hibernate `show_sql`, SQLAlchemy `echo`, Prisma `log: ['query']`). Run the target request or test and count statements.
2. Find N+1 patterns: a loop that touches a relation, a serializer or template that reads a related field, a property that queries on access. Confirm by seeing one parent query followed by N near-identical queries.
3. Fix with the ORM's eager loading: join or prefetch the relation in the one query that loads the parents. Use batch loading when a join would multiply rows.
4. Find over-fetching: whole rows loaded to read two fields, or entire tables loaded to count or check existence. Select only needed columns; use `count`, `exists` and aggregates in the database, not in memory.
5. Find missing transactions: two or more writes that must succeed together, read-then-write sequences, counters updated in memory. Wrap them in a transaction, and lock the row (`select for update`) or use an atomic update expression when two requests could collide.
6. Look for unbounded queries without limit or pagination, and filters on columns with no index.
7. Re-run and show the statement count before and after.

## Rules

- Never fix a query you have not seen the SQL for.
- Keep fixes local; do not rewrite the data layer.
- Keep transactions short and free of network calls.

## Report

Per finding: file and line, statements before and after, and the change.