N+1 occurs when one query loads parents and an additional query runs for each parent. Detect it through SQL logging/tracing and query counts, then shape the required data in one bounded query or a deliberate small set. Example: var orders = await db.Orders.AsNoTracking() .Where(o => o.RestaurantId == tenant.RestaurantId && o.OutletId == tenant.OutletId) .Select(o => new OrderDto(o.Id, o.Lines.Count, o.Lines.Sum(l => l.Total))) .Take(100).ToListAsync(ct); Blind Include chains can create cartesian explosions and huge payloads. Prefer projection; consider split queries for multiple collections and verify generated SQL. Disable accidental lazy loading in APIs when it hides database access. Add integration tests or telemetry for query count and latency.
Backbencher Developer · · 0 likes