Eager Loading
Problem
- An ASP.NET Core API returns 100 orders. For each order, it fetches that order's customer with a separate query.
- One query loads the orders. Then, for every order, another query fetches its customer, one at a time.
- A single API request now runs 101 queries, most of them spent waiting on network round trips to the database.
One request, one hundred and one queries.
Solution
Eager Loading
Load orders and customers together in one query, joined on the customer id.
The same 100 orders come back with their customers already attached, in a single round trip.
If a loop runs a query, the loop count is your real query count, which is exactly what eager loading avoids.
Analogy
You need groceries for the week. You could drive to the store, buy one item, drive home, and repeat a hundred times.
Or you write the full list and make one trip. Same groceries, one journey.
Eager loading is shopping with the full list.