Project
Tune It
Goal: A million-row shop, ten slow queries from the ticket queue, and one method applied ten times: measure, read the plan, find the cause, fix it, measure again — writing up before/after for each. Time: ~6–8 hours
The brief
You've inherited bigshop — 200,000 customers, 10,000 products, 1,000,000 orders, and no indexes beyond the primary keys. Ten complaints are waiting. For each one you deliver a short write-up: the symptom, the plan that proves the cause, the fix, and the plan (and timing) after.
| File | What it holds |
|---|---|
gen.sql |
The data generator (~7 seconds, ~200 MB) |
slow-queries.sql |
The ten tickets, verbatim |
fixes.sql |
The reference fixes — open it only after your own diagnosis |
$ createdb bigshop
$ psql -d bigshop -f gen.sql
$ psql -d bigshop -- and start on ticket Q1
The method (apply it ten times)
- Run it with evidence:
EXPLAIN (ANALYZE, BUFFERS)— wrapped in BEGIN/ROLLBACK if it writes. Note the time. - Find the dominant node — where did the rows or the buffers go? Compare
rows=withactual rows=. - Name the cause in one sentence. "Seq scan because the predicate wraps the column in extract()" — if you can't say it, you haven't found it.
- Apply the smallest fix — an index, a rewrite, a statistics object. One change.
- Re-run step 1. Keep the numbers. If it didn't improve, undo and rethink — don't stack guesses.
What the ten tickets teach
Every ticket maps to a lesson you've had — that's the design. Expect to meet, in some order: the missing FK index (and a composite that serves the ORDER BY too), the lower(email) expression-index classic, a leading-wildcard search that wants trigram GIN, an extract() predicate begging to be a date range, deep-OFFSET pagination, a NOT IN that is both slow and a correctness time bomb, a partial-index slice, correlated columns lying to the planner, a SELECT * dragging a wide column through a join — and one ticket whose honest answer is "the plan is already right; close it." Knowing when to stop is part of the method.
Self-check (verified on a real run)
After fixes.sql, these plans flip exactly like this:
| Ticket | Before | After |
|---|---|---|
| Q1 customer history | Parallel Seq Scan + Sort over 1M rows |
Index Scan using idx_orders_customer_date — no sort |
| Q2 login lookup | Parallel Seq Scan, filter on lower(email) |
Index Scan using idx_customers_email_lower |
Q3 %model 77% search |
Seq scan | Bitmap Index Scan on idx_products_name_trgm |
| Q7 pending dashboard | Seq/bitmap over all statuses | Bitmap Index Scan on idx_orders_pending_date (partial) |
| Q8 Pune segment | estimate ≈ 1/16 of table vs actual 1/4 | estimate within ~1% after CREATE STATISTICS + ANALYZE |
Your write-up carries the timings — they vary by machine; the plan shapes don't.
Stretch goals (optional)
- Add
pg_stat_statementsto the server, replay all ten queries twenty times, and rank the tickets by total time — does the queue order change? - Find the one redundant index your fixes created (hint: Part 17's "(a,b) covers (a)" rule) and justify dropping or keeping it.
- Re-run
gen.sqlwith 5 million orders and re-verify your fixes still hold.