jargon

Comparison

Lazy loadingvsN+1 query

Lazy loading

you looped over a hundred orders reading customer.name, and the log showed a hundred and one queries.

Deferring the loading of an associated object until it is first accessed, usually through a virtual proxy or a marker on the field. It makes a naive read cheap and makes the cost of a loop invisible, which is the entire mechanism behind the N+1 query problem. It also produces the other classic failure — touching a lazy field after the session has closed, which fails at a point far from the query that caused it.

Full entry →

N+1 query

you fetch a list of fifty orders and then the ORM quietly issues fifty more queries, one per order, to load the customer.

A pattern where fetching a collection triggers one additional query per element, usually via lazy-loaded ORM associations. Latency scales with result size instead of staying flat, so the endpoint is fine in development with three rows and terrible in production with three hundred. The fix is an eager load, a join, or a batched second query.

Full entry →

Related comparisons