โ Back to The N+1 query problem (JPA and Hibernate)
๐ฝ๏ธ The N+1 query problem (JPA and Hibernate): cheat sheet
๐จ The analogyA waiter who walks back to the kitchen once per diner for each side dish, instead of carrying one tray.
๐ The regular explanationHow an ORM turns one query for a list of entities into one more query per entity when code touches a lazy or eager association, how to spot it, and how JOIN FETCH, entity graphs, batch fetching and DTO projections fix it.
- The table orders, and the waiter makes one trip to the kitchen for all the main dishes. One ticket goes on the spike.
- The first diner asks for chips. The waiter walks back to the kitchen for that one bowl. A second ticket.
- Then the second diner asks, then the third. Each time, another walk to the kitchen and another ticket.
- Four trips for one table. With sixty diners it would be sixty-one trips, and the whole room would wait.
- With the first way of working, the waiter brings only the main dish and leaves a ticket on the plate where the chips will go.
- Only when the diner asks for the chips does the ticket send the waiter back to the kitchen.
- With the second way, serving one diner, the waiter carries the main and its side together in a single trip.
- But for a whole table he brings all the mains, then still walks back once per diner for their side. Always bringing sides didn't save a single trip.
- The waiter serves one table during a practice run. Every trip leaves a ticket on the spike, and the same ticket keeps coming back with a different diner on it.
- The tally card counts every trip, so nobody has to read the tickets one by one.
- The manager's rule says one table takes at most one trip. He checks the tally, sees four, and the practice run fails before real diners ever wait.
- The table says up front that everyone wants their sides. The waiter carries all the mains and all the sides on one tray. One trip, one ticket.
- This table always orders that way, so it's written on the order pad once. The waiter reads the pad and brings one full tray without being told.
- When the first diner asks for chips, the waiter brings chips for sixteen diners in one go. A handful of trips instead of sixty-one.
- If the table only wants to read the bill, the waiter fetches no dishes at all, just a printed list of names and prices.
- The table asks for only the first ten diners for now. With all the sides on one tray, the waiter carries out food for the whole room and picks out ten at the counter.
- Better to ask first who the ten diners are, then bring just their food on one tray.
- Now the table wants salads and drinks on the same tray, both as loose heaps. The waiter refuses before setting off, because he couldn't tell a real second helping from a repeat.
- With the sides labelled he'll carry them, but the kitchen plates every salad with every drink. Three of each makes nine plates per diner, and the tray groans.
- The fix is one kind of side per tray. Two light trips beat one overloaded tray.
- The waiter serves the mains, and right after, his shift ends.
- A diner asks for chips. Nobody on shift can go to the kitchen, so the diner gets an angry "sorry, can't".
- This restaurant keeps the waiter on shift until the diners leave. The chips arrive, but with one hidden trip per diner, and a kitchen line stays tied up the whole time.
- Better to decide what the table needs while the waiter is still on shift, and bring it all on one tray.
๐งฉ Key questions
- The waiter brings the mains for 50 diners in one trip, then each diner asks for chips. How many trips does he make in total?
- Fifty-one: one for the mains, then one per diner. Each request for chips sends him back to the kitchen on his own, so it's one trip plus one per diner.
- To save trips, someone tells the waiter to always bring the sides, whatever happens. What changes when the whole table orders?
- He still walks back once per diner for their sides, just without being asked. Always bringing the sides only means he doesn't wait to be asked. Only telling him the whole order up front gets it onto one tray.
- The table wants only the first ten diners served, and the waiter puts everyone's sides on the same tray. What goes wrong?
- He brings the whole room's food and picks out ten at the counter. He can't tell where diner ten ends on a shared tray, so he takes everything. Asking who the ten are first, then fetching their food, fixes it.
- A restaurant keeps the waiter on shift until the diners leave, so late requests for chips never get a "sorry, can't". What's the catch?
- Each late request is still its own trip, now hidden, and a kitchen line stays tied up. Staying on shift hides the angry answer but not the trips. Better to decide what the table needs while he's still on shift.