Backend development

Stage 2 · Lesson 10 of 10

The N+1 query problem

Relational databases and SQL

does not load related data unless you ask for it. If proxies are turned on, it loads each relation the moment you touch it. That produces the problem: load 50 orders, read order.Customer in a loop, and 50 more queries run.

Load related data on purpose with Include and ThenInclude. Better still, use Select to copy exactly the fields the response needs into a DTO. That produces one tight query.

Log the generated SQL in development with LogTo or the standard logging categories. For large collections, AsSplitQuery avoids one huge .

Common trapThe trap without lazy loading is a missing Include. The is simply null, so the bug looks like missing data rather than a slow query.

A question to check yourself, an AI practice session and 1 cheat sheet entry come with the full roadmap. Unlock

Resources for this stage

Start with· follow one of these

Practise

Also worth your time

Mark this stage as not done?