Skip to content

Lab 03 — Composite Indexes

Source reference

This lab extends the composite-index and SARGability guidance in SQL/Query Optimization with PostgreSQL column-order and write-cost experiments.

Scenario

The Orders API filters by customer and status, orders by newest first, and returns 50 rows. A useful index must account for filtering, ordering, and the position of equality and range-like requirements.

Run the exercise

make shell
\i sql/labs/03-composite-indexes/bad_queries.sql

Investigate

  1. Record scan, sort, row, loop, and buffer evidence.
  2. Determine which predicates are equality conditions.
  3. Check whether PostgreSQL sorts explicitly or reads in the needed order.
  4. Compare candidate column orders using the leftmost-prefix rule.
  5. Consider write cost and redundant-index risk before proposing a change.

Diagnostic question

Which column order supports this exact filter-and-order pattern, and which parts of the plan should disappear or shrink if your hypothesis is correct?

Previous: Index access paths · Next: Join optimization