The N+1 Query Problem and Fetching Strategies
Diagnose and cure the number one ORM performance killer using JOIN FETCH, @EntityGraph, and DTO projections.
01The supermarket grocery run
Imagine you are making dinner for a large family. You need 50 ingredients: pasta, sauce, tomatoes, cheese, garlic, olive oil, and so on.
A sensible person takes a shopping cart, walks down the supermarket aisles, collects all 50 items in one basket, pays once at the cash register, and drives home.
Now imagine someone who drives to the supermarket, buys one box of pasta, drives home, puts it on the counter, drives back to the supermarket, buys one jar of sauce, drives home, and repeats this trip 50 times.
That absurd 50-trip scenario is exactly what happens on your database when code suffers from The N+1 Query Problem.
Loading the rest of the lesson…