How it works · 12 of 38 Foundation
What is a database index and why does a database index make lookups fast?
Why some lookups are instant
Indexes: An index is a sorted copy of one column, kept as a tree of pages, so the database can open three or four pages to find a row instead of reading the whole table; every index must also be updated on each write.
Loading the animation…
Say it like this An index is a sorted copy of a column kept as a tree, so the database opens three or four pages instead of reading every row; each index also has to be updated on every write, so index the columns you search by.
Under the hood — for builders
- The source's own example
- PostgreSQL docs: for SELECT content FROM test1 WHERE id = constant; "with no advance preparation, the system would have to scan the entire test1 table, row by row". After CREATE INDEX test1_id_index ON test1 (id); it can follow the index to the matching rows.
- Rows grow, levels barely move
- Pages read to find one row through a B-tree index, if each 8 kB page holds about 300 entries (real counts depend on the key size).
- Terms
- Page: the fixed block a database reads from disk, 8 kB in PostgreSQL. B-tree: the default index, a tree of sorted pages. Full table scan: reading every row (EXPLAIN shows Seq Scan). Composite index: one index over two columns, e.g. (city, date). Selectivity: how few rows a value matches; an index helps most when it is few.
- Watch out
- Every index slows writes: "the system has to keep it synchronized with the table" (PostgreSQL docs), so drop unused ones. A column with three values (status) rarely helps. WHERE lower(email) = … skips a plain index on email.
- Measure it
- Run EXPLAIN on your slowest queries: Seq Scan on a big table is the cue. In PostgreSQL, pg_stat_user_indexes with idx_scan = 0 lists indexes nobody uses.
- Sources
- PostgreSQL documentation: "Indexes: Introduction" and "Database Page Layout" (read November 2026).
What happens in the indexes animation, step by step?
- A table of 10 lakh orders. Find order #734912. With no index, the database cannot know where it is, so it reads row after row: up to all 10 lakh.
- An index is a sorted copy of one column, kept as a tree of pages. Each page holds a few hundred entries and says which page to open next.
- Find #734912 again: the top page points to the right branch, three page reads in all, and a thread drops to the one row. Nothing else is read.
- Your turn: drag the table from a thousand rows to a hundred crore. Watch both counters.
- So index every column? Each index is one more sorted copy to update whenever a row is written, and more disk. One new order here means six writes, not one.
- Writes now touch many places. What if two buyers grab the last seat at the same instant, and one write lands half-done? Next: transactions.
What everyday example explains indexes?
The index at the back of a textbook. To find "photosynthesis" you don't read all 400 pages; the index sends you to page 212. But every time the book gains a page, someone must update the index too, and an index for every word would be longer than the book.
Why should a team care about indexes?
When a screen gets slow as data grows, check whether its query has an index on the columns it filters by; when writes get slow, look for indexes nobody uses, because each one costs every insert and update.