Skip to content

Lab 04 — Join Optimization

Scenario

A regional report joins customers, orders, order items, and payments for recent orders. One-to-many relationships can multiply rows before aggregation and may change both correctness and cost.

Run the exercise

make shell
\i sql/labs/04-joins/bad_queries.sql

Investigate

  1. Trace actual rows through every join node.
  2. Identify nested-loop, hash, or merge joins and explain why each was chosen.
  3. Compare estimated and actual row multiplication.
  4. Verify whether joining both items and payments changes the intended count.
  5. Locate the node responsible for the most work rather than blaming the top aggregate.

Diagnostic question

Is the report slow because of a poor join algorithm, excessive intermediate rows, or both—and is its result semantically correct?

Previous: Composite indexes · Next: Cardinality