1.What an index is, and when it hurts
An index is a second, sorted copy of one or more columns, kept alongside the table and pointing back at the rows. It turns "look at every row until you find it" into "jump straight to it". It hurts when the database has to maintain it on every write, or when the query it is meant to help matches most of the table anyway.
Key features
- An index stores the indexed column sorted, with a pointer back to the row — the table itself stays in whatever order rows were written
- A sorted structure finds one row in about log2(n) comparisons: roughly 17 for 100,000 rows, against 100,000 for a full scan
- Every index is written on every INSERT, UPDATE of an indexed column, and DELETE — five indexes means six structures touched per insert instead of one
- An index only helps a predicate that is selective: a condition matching 95% of the table is cheaper to answer by scanning, and the planner will scan
- A composite index on (a, b) can serve a query filtering on a, or on a and b — but not one filtering on b alone, because b is only sorted within each a
- The index also has to be kept in memory to be fast; indexes you never query still consume cache the ones you do query wanted
Signature
index = sorted(column) -> row pointer · log2(n) seek · one write per index per row change
An index is a sorted copy of a column that turns a full scan into a seek — about 17 comparisons instead of 100,000 on a 100k-row table. It costs a write on every insert, update and delete, and it buys nothing for a predicate matching most of the table. Index from the slow-query list, not from the column list.