Loading...
Loading...
Browse 4 real-world technical and behavioral interview questions about N plus one. Review scenarios, edge cases, and architectural best practices.
The repository call fetches parents in one query; the extra queries come later, when something touches a lazy association - usually the JSON serialiser, with open-session-in-view keeping the context alive so it succeeds silently. Diagnose by counting statements per request, then fix with a join fetch or entity graph, batch fetching, or a DTO projection.
An N+1 happens when the ORM follows a relation lazily once per row; select_related fixes forward foreign keys with a SQL join, prefetch_related issues one extra query per relation and joins in Python. An async endpoint gets slower when it contains a blocking call, because that stalls the whole event loop.
You get N+1 when one query loads N rows and something later dereferences a lazy association on each of them, firing a select apiece. Fix it per call site with join fetch, an entity graph, or batch fetching - not by changing mappings to EAGER.
EF Core query performance starts with the generated SQL, then checks N+1 queries, cartesian Include joins, over-fetching and change tracking. Use projections and AsNoTracking when the result is read-only.