Databases
Indexes: multiple-choice review
Six questions that cut across the whole unit. None is a definition to recite — each mirrors a design call or an incident you weigh under real query load, where the wrong index costs storage, write throughput, or a 3am page.
Confirm you can connect B-tree anatomy, the leading-column rule, partial and covering indexes, index-only scans, index-type selection, and production failure modes — the synthesis the seven lessons built toward.
A multi-tenant dashboard has an index on (workspace_id, status, created_at). A new analytics query runs WHERE status = 'pending' ORDER BY created_at DESC with no workspace_id filter, and it does a sequential scan. What is the fix that respects the unit's design discipline?
A 100M-row tasks table is 80% status = 'done' (never queried by the dashboard). The dashboard only reads open and in_progress tasks. Which index strategy minimizes both size and write overhead?
EXPLAIN (ANALYZE, BUFFERS) shows Index Only Scan with Heap Fetches: 12000 on an orders table, and the query is slow despite the right covering index. What is happening and what is the durable fix?
A DELETE FROM posts WHERE id = 42 with ON DELETE CASCADE takes 7 minutes and holds locks the whole time, on a posts/comments schema. What is the root cause?
orders.user_id is BIGINT with an index on orders(user_id). A query runs SELECT * FROM orders WHERE user_id = '42' (the literal is TEXT) and does a Seq Scan. Why, and what is the fix?
A search feature runs WHERE title ILIKE '%invoice%' on a 50M-row table and takes 12 seconds. The query is keyword-style search over document text. What is the right index choice?
The unit’s through-line is one design loop: a composite is usable only from a leading prefix, so design it around the always-present filter; partial predicates cut a hot-subset index to a fraction of full size and skip writes for cold rows; INCLUDE plus a current Visibility Map make an index-only scan skip the heap; the index type must match the data shape (B-tree for ordered values, GIN for text and JSONB, HNSW for semantic); and the recurring failures — missing FK index, type coercion, stale stats, leading-wildcard ILIKE — all resolve back to matching the index to the exact question the query asks, verified with EXPLAIN (ANALYZE, BUFFERS).