Many Small Queries in a Single View

A page can load its parent list once and then issue a separate query for information attached to each row. The number of operations grows with result size.

Source
Anbloq
Language
English
Created
Updated
Chapters
4
Reading time
1 min
In this article4 chapters
  1. Trace three parents
  2. Verify the new output
  3. Compare one parent query and many child lookups
  4. Check payload size and total cost

Trace three parents

Fetching a list and one related count each for three categories takes four calls in the simple implementation. A grouped query can supply counts together. See counting children by parent.

Verify the new output

Check empty categories, duplicates, and the chosen views after consolidation. The changed query grain must still supply the intended result.[1]

Compare one parent query and many child lookups

A list receives ten authors from one query and then separately loads books for each. That can produce eleven database round trips. A suitable batched association query can reduce trips while preserving parents with no books. See counting related records.

Query 1: load 10 parents
Queries 2–11: one child load each
Batched alternative: parent query plus one planned child query
Verify mapping of records back to parent IDs

Check payload size and total cost

Fetching every related field can add more bytes than the view needs. Test parents with no children and several children, then measure count, rows and end-to-end latency.

References

  1. Google, Site Reliability Engineering ↩