Skip to catalogue

45

N+1 query

One query to list N rows, then one query per row. Fine at 3. Dead at 300.

What is N+1 query?

N+1 is a loop that queries per item after fetching the list. ORMs do this when you lazy-load relations in a template.

Why does N+1 query matter when vibe coding?

The model writes a list page that looks right with seed data. Then a real workspace has 400 projects and the page takes twelve seconds. Name N+1 or you will ship it.

How do you do N+1 query?

For any list, one query with a join or an `include`. Log SQL in dev. If you see a query per row, that is the bug, not “the DB is slow.”

How do you ask a model for N+1 query?

No N+1. The (list) page must not query per item. Load relations in one query. If you use an ORM, include/join them up front.

What goes wrong with N+1 query?

Caching the N+1. You cached the wound. Fix the query shape.

adjacent