Stage 2 · Lesson 10 of 10
The N+1 query problem
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
- CoursePaidGrokking Relational Database Design and Modeling
Design Gurus · Intermediate · $98 one-time
The modelling and normalisation decisions that stay with a codebase for years
- CourseFreeCS50's Introduction to Databases with SQL
Harvard University · Beginner
Free course from querying through schema design, indexes, transactions and scaling
Practise
Also worth your time
- Database Indexing Explained: B-Tree, Hash, Bitmap, R-Tree, and When to Use Each (opens in a new tab)Read
B-tree and composite indexes, plus the leftmost-prefix rule that trips people up
What sits under your queries: storage engines, transactions and isolation levels