Eager Loading
PronunciationEE-ger LOH-ding
Definition
Eager loading fetches related rows together with the main query, instead of fetching them one by one later when your code touches them.
Where you hear it
In ORM performance work, code reviews that fix slow list pages, and Django's select_related and prefetch_related.
Examples
Eager loading the member with each payment turned 101 queries into one.
Use eager loading before looping over the related objects.
With eager loading, the orders page runs one query instead of one per customer.
Common mistake
Eager-loading everything everywhere. Load only the relations you will really use, or you fetch data you never read.
Don't confuse with
Lazy loading, which fetches a relation only when you first access it. It is convenient but is the cause of N+1 queries in loops.
Say it at work
- In a meeting or chat
Add select_related here to load the member eagerly.
- In an email or ticket
This page does one query per row; eager load the relation.