Scenario: 30-Second Dashboard in SQL
Measure-first triage — access before rewrite
EXPLAIN ANALYZE first — misestimated node, missing index, or fan-out. Then sargable predicates, covering/composite indexes, pre-aggregation (rollup tables, matviews), result caching. Fix access paths before rewriting logic. Document RPO of staleness when precomputing — dashboards tolerate what balances cannot.
EXPLAIN the node
ANALYZE + BUFFERS — misestimated/scan-heavy node first.
Fix access once
One variable: stats, index, or sargability — re-measure.
Precompute hot
Rollups/matviews/caches for proven shapes with staleness contracts.
Shape + narrow
Filter-before-widen, narrow projections, keyset pages.
1-- triage order:2-- 1. EXPLAIN (ANALYZE, BUFFERS) ...3-- 2. CREATE INDEX ... / ANALYZE ...4-- 3. MATERIALIZED VIEW ... + REFRESH schedule
| phase | action |
|---|---|
| diagnose | plan gap |
| access | index/stats |
| precompute | rollup/cache |
Same dashboard, three levers in order. Skipping to caching without fixing a missing FK index cements the wrong plan under a faster cache.
Triage in order
Access before rewrite, proof before keep.
1-- 1. EXPLAIN (ANALYZE, BUFFERS) ...2-- 2. CREATE INDEX / ANALYZE / rewrite predicate3-- 3. MATERIALIZED VIEW + schedule
| phase | outcome |
|---|---|
| diagnose | gap found |
| access | fixed + proven |
| precompute | hot cached |
Width win (index-only)
Narrow projection unlocks covering seeks.
1-- SELECT name, salary ... (covered) vs SELECT * (heap)
| projection | plan |
|---|---|
| covered | index-only |
| star | heap fetches |
Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Take the queries you just visualized and see the data structures that power real databases.