Learn SQL
-- advanced.dashboard_triage_scenario

Scenario: 30-Second Dashboard in SQL

Measure-first triage — access before rewrite

--concept

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.

--how it works
1

EXPLAIN the node

ANALYZE + BUFFERS — misestimated/scan-heavy node first.

2

Fix access once

One variable: stats, index, or sargability — re-measure.

3

Precompute hot

Rollups/matviews/caches for proven shapes with staleness contracts.

4

Shape + narrow

Filter-before-widen, narrow projections, keyset pages.

--visualization
DIAGNOSEACCESSPRECOMPUTE
Stage 1 / 3
Diagnose: EXPLAIN ANALYZE gap — never guess-index.
--example
query.sql
1-- triage order:
2-- 1. EXPLAIN (ANALYZE, BUFFERS) ...
3-- 2. CREATE INDEX ... / ANALYZE ...
4-- 3. MATERIALIZED VIEW ... + REFRESH schedule
Result
phaseaction
diagnoseplan gap
accessindex/stats
precomputerollup/cache

Same dashboard, three levers in order. Skipping to caching without fixing a missing FK index cements the wrong plan under a faster cache.

--variants

Triage in order

Access before rewrite, proof before keep.

query.sql
1-- 1. EXPLAIN (ANALYZE, BUFFERS) ...
2-- 2. CREATE INDEX / ANALYZE / rewrite predicate
3-- 3. MATERIALIZED VIEW + schedule
Result
phaseoutcome
diagnosegap found
accessfixed + proven
precomputehot cached
1Caching a wrong plan cements it faster.
2Index proof is before/after plans.
3Staleness tolerance signed before caching.

Width win (index-only)

Narrow projection unlocks covering seeks.

query.sql
1-- SELECT name, salary ... (covered) vs SELECT * (heap)
Result
projectionplan
coveredindex-only
starheap fetches
1INCLUDE payloads cover the dashboard columns.
2Visibility fresh → heap untouched.
3SELECT * defeats the covering.
--key facts
Estimates-firstmethodology sentence
Access firstrewrite second
Stale contractdashboards yes, balances no
--use cases
On-call dashboard fires with runbook order
Pre-launch perf sign-off with plan evidence
--under the hood
BUFFERS hit/read splits cache-cold vs cache-hot — size work_mem + shared_buffers from evidence.
Concurrent REFRESH needs unique index; else ACCESS EXCLUSIVE blocks readers.

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Take the queries you just visualized and see the data structures that power real databases.