Indexes and what they cost
An index is the reason a query over a billion rows can return in two milliseconds — and also the reason your writes mysteriously got slow after "someone optimised the database." Both facts come from the same definition: an index is a second data structure, sorted by a column of your choosing, that trades write speed and storage for read speed. Like the index at the back of a textbook: finding "photosynthesis" takes seconds instead of a page-by-page scan — but someone had to build that index, and every edit to the book means updating it.
What you pay — because there is no free index
- Every write pays every index. An INSERT into a table with ten indexes performs eleven writes: the row, plus an entry into each index's sorted structure. UPDATEs and DELETEs likewise. Watch a write-heavy table accumulate indexes over the years and you can watch its write latency climb in lockstep.
- Indexes eat memory. An index only delivers its speed if it's (mostly) resident in RAM. An index too big for memory does disk seeks to traverse itself — which was the exact thing it existed to prevent.
Composite indexes and the leftmost rule
An index can cover multiple columns: (tenant_id, created_at). Here's the mental model that makes everything click: this index is a phone book sorted by surname, then first name. Finding all "Sharma"s? Easy — they're contiguous. Finding "Sharma, Priya"? Easy — sorted within Sharma. Finding everyone named Priya regardless of surname? Useless — Priyas are scattered across every surname. You'd scan the whole book.
Exactly so: the index serves WHERE tenant_id = 5, and WHERE tenant_id = 5 AND created_at > ... — but not WHERE created_at > ... alone, because entries are sorted by tenant first. This leftmost-prefix rule is the number one reason a query "has an index" and still does a full table scan. When someone says "but there's an index on that column!", check whether the column is leftmost.
Covering indexes: never touching the table at all
Normally an index lookup is two hops: find the entry in the index, then jump to the table for the full row. But if the index itself contains every column the query needs, the second hop vanishes. An index on (user_id, status, created_at) fully answers SELECT status, created_at WHERE user_id = ? — the table is never touched. On a hot query this is routinely a several-fold win, and it's why you'll see indexes that deliberately include "extra" columns.
Selectivity: when the database ignores your index (correctly)
Index a boolean column that's 50/50 true/false and the database will likely refuse to use it. This looks like a bug and is actually wisdom: fetching half the table via index means millions of scattered random reads, which is slower than one sequential scan of everything (Tier 0 strikes again: sequential beats random). Indexes shine on selective columns — ones where a value narrows results to a small fraction. The query planner knows this arithmetic; when it "ignores your index," it usually did the math you didn't.
Read the plan — it's the database explaining itself
EXPLAIN ANALYZE shows the actual execution plan with real timings, and reading it is not optional for anyone who touches a database. Two things to hunt for: sequential scans on large tables (a missing or unusable index), and large gaps between estimated and actual row counts — that means the planner's statistics are stale, it's choosing plans based on a fantasy of your data, and a stats refresh (ANALYZE) may fix "sudden" slowness no code change caused.
Adding an index for every slow query until writes collapse under the accumulated weight. Indexes are individually cheap and collectively expensive. Audit them: most databases expose per-index usage statistics, and it is entirely normal to discover indexes that have never been used once — pure write tax, zero benefit. Deleting a dead index is one of the safest performance wins available.