Batch loading

Collecting the ids of every parent row and fetching all their children in one query with IN (...), then grouping the results in memory by parent id. The query count follows the number of tables, not the number of rows.

Minimal example

sql
SELECT item_id, size, price_paise
FROM item_sizes
WHERE item_id IN (?, ?, ?)
ORDER BY item_id, price_paise;

Learn this concept

Lesson 2.11 · N+1 queries and how to batch them

Used in exercises

Exercise 2.12 · GET /v1/restaurants/{id}/menu in five queries Exercise 2.14 · POST /v1/menu/price: price a configured item
Prerequisite conceptsN+1 queries