.NET Architecture & System Design Interview Question #85

What is the N+1 query problem?

Transactions, CQRS, Event Sourcing & Distributed Consistency Mid-Level Intermediate

Quick Interview Answer

The N+1 query problem occurs when an application executes one query to obtain a collection and then executes an additional query for each item, creating many database round trips.

Detailed Explanation

The N+1 query problem occurs when an application executes one query to obtain a collection and then executes an additional query for each item, creating many database round trips.

It commonly appears through careless lazy loading or per-row lookups. Fixes include appropriate projection, eager loading, joins, batching, or redesigning the query to fetch the required shape efficiently.

Code Example

1 query: load 100 orders
100 queries: load customer for each

Total = 101 queries

Prefer projection/join/batching.