Database indexes
Find rows without scanning the whole table, and why every index makes writes a little slower.
Why indexes matter
Without an index, finding rows that match a condition means reading the whole table (a full scan). An index is a separate sorted structure, usually a B-tree, that maps column values to row locations, the way a book's index maps words to pages.
Try it
Grow the table and compare a full scan with an indexed lookup. Then add indexes and see what it costs writes.
Kinds of index
- B-tree (the default): equality and range queries (
>,BETWEEN,ORDER BY). - Hash: equality only, very fast.
- Composite (several columns): an index on
(user_id, created_at)answers "this user's latest posts" directly. Column order matters: it helps queries that filter onuser_idfirst. - Covering: includes every column the query needs, so the table itself is never read.
- Full-text and geospatial indexes for search and "nearby" queries (or a separate search engine).
The trade-off
- Every insert, update and delete must also update every index on the table.
- Indexes take storage and memory; an index that doesn't fit in memory is much slower.
- Index the columns you filter, join and sort on in hot queries, and nothing else. Check with
EXPLAIN.
In the interview
- When you write a schema, say which columns are indexed and why: "index on (conversation_id, sent_at) to page through messages".
- For write-heavy tables, keep indexes few.
- Mention composite indexes and that column order follows the query.