Course overview/Design queries that survive review4 of 4
Read a query fast
Read a query fast
Annotate grain in two passes so fan-out becomes visible before you run the query.
Read grain before detail
Write beside every CTE and join what one row represents. On the first pass, ignore filters and column lists. On the second, check filters, boundaries, and selected expressions. Reading join conditions aloud makes it harder to skim past the place where grain changes.
drill-3.sql joins customer rows after it has reached order-line grain. Because 10 customer IDs are duplicated, the lines_with_customer step repeats those lines. The query returns 373,614.70 as written and 348,521.23 after only that join is deduplicated.
Your task
Make annotated copies of all three reading drills. Record your times. Identify the query and step with the grain error, then run it as written and with only the faulty step fixed to report both totals.
Check your understanding
- What do you write beside a CTE when you annotate grain?
- A query joins
orderstoorder_itemsand then sumsorder_total. Reading only the annotations, how do you know it is wrong? - Why does the first pass ignore filters?
Do it with your agent
Say next lesson, annotate before running, then say review my work. The agent should look for a grain sentence on every CTE and join, not only a final verdict.