The Detail-Backfill Pattern or DataLoader Before It Was Cool
While optimization is often premature and, therefore, the root of all evil[1], using an efficient design for data access is choosing a good path rather than paving a bad one. A common task for APIs is to retrieve a “page” of master rows and their detail rows (e.g. Invoice and Items), as well as a total count of the master rows that match the given criteria. Here are three ways to get the data from a standard SQL database:
Option 1: for each row of the master table query result, execute a query per detail table to get the detail rows for that master row (also known, derisively, as N+1)
Pro
- initiative design
- allows easy pagination of master rows
Con
- slow
- lots of little queries burden SQL server and network
- data might change during the process if the option does not include a potentially huge transaction (because the last query returns long after the first one)