03. Data & Storage / Lesson 2 of 11

Indexes

A sorted shortcut that turns a slow search into a fast lookup.

  • 10 min
  • Intermediate
  • 10 steps
  • Rows checked10,000,000 (bad)
  • Query time2,000 ms (bad)
  • Per insert—

Scan

WHERE email = 'ana@...'

✗ 10,000,000 rows checked

✗ ≈ 2,000 ms

Appusers table (10M rows)⚠ scanning all rows
  • Request
  • Response
  • Write
Step 1/10

Step 1 of 10: Without an index, the database reads every row

Read every step as text
  1. Step 1: Without an index, the database reads every row

    The users table has 10,000,000 rows, and we run SELECT * FROM users WHERE email = 'ana@example.com'. Rows are in no useful order, so the database must check them one by one: a full table scan.

    Say it checks 5 million rows a second. One query then takes 2,000 ms. At 10 such queries a second, the database would need 20 seconds of work every second, which is far more than one CPU has.

    A query that is fast on 1,000 test rows can be very slow on 10 million. The cost grows with the table.

  2. Step 2: Idea: keep a sorted copy of the column

    Think of the index at the back of a book: topics in alphabetical order, each with a page number. A database index does the same. It keeps the emails sorted, and next to each one a pointer to its row.

    In a sorted list we can use binary search: look in the middle, throw away the wrong half, repeat. For 10,000,000 entries that is at most 24 checks, not 10,000,000.

    An index is an extra structure, kept up to date by the database, that trades storage and write time for read speed.

  3. Step 3: Real indexes are B-trees

    A plain sorted list is slow to update and awkward on disk. So databases use a B-tree: a tree where each node is one disk page holding hundreds of keys, about 500 here. Each key points down to a child node.

    With 500 children per node, level 1 reaches 500, level 2 reaches 250,000, and 3 levels reach 125,000,000. So 10,000,000 rows need a tree only 3 levels deep.

    The tree stays shallow and balanced as the table grows. 125 million rows still fit in 3 levels, and the database keeps the top levels in memory.

  4. Step 4: One lookup: walk down, then fetch the row

    The query now starts at the root page, follows one child per level (3 pages), and reads the leaf entry for ana@example.com. Then it follows the pointer to the table page: 4 page reads in all.

    At 0.1 ms for each page read from an SSD, that is 0.4 ms instead of 2,000 ms: about 5,000 times faster, and it barely grows as the table gets bigger.

    In SQL you create it with one line: CREATE INDEX idx_users_email ON users (email);

  5. Step 5: Sorted order also makes ranges and sorting cheap

    Because the leaves are in order and linked, WHERE created_at >= '2026-11-01' AND created_at < '2026-11-02' (an index on created_at) finds the start in 3 pages and then reads along the leaves until the end. ORDER BY created_at needs no sort at all.

    Say 500 users signed up that day, and rows saved together sit near each other (10 per page). That is 4 pages for the search plus 50 for the rows: about 5.4 ms.

    A hash index finds exact matches only. B-trees are the default because they handle ranges, sorting and exact matches.

  6. Step 6: Composite indexes: the column order matters

    An index can cover several columns, for example (country, city, created_at). It is sorted by country first, then by city inside each country, then by date. You can see it as a phone book sorted by last name, then first name.

    It helps queries that use a left prefix: country, or country + city. A query on city alone cannot jump to a start point, because cities are scattered across all the countries, so it falls back to scanning.

    Put the columns you compare with equals first, and a range column last. Check what the database really does with EXPLAIN.

  7. Step 7: Covering index: answer from the index alone

    If the index already holds every column the query needs, the database never opens the table. For SELECT name FROM users WHERE email = ..., an index on (email) INCLUDE (name) returns the name straight from the leaf: 3 page reads, 0.3 ms.

    This is called an index-only scan. It saves one read per row, which adds up for queries that return many rows. The index gets bigger, because it stores the extra column.

    Cover your hottest queries, and keep the extra columns few and small.

  8. Step 8: Every index slows down writes

    An index is a second copy of part of the data, and the database must keep it right. Each INSERT writes the table row and an entry into every index. Each UPDATE of an indexed column moves an entry, and each DELETE removes one.

    With one index, an insert writes 2 page writes: one for the table, one for the index. A page split in a full B-tree page adds a little more.

    Reads get faster, writes get slower. For read-heavy tables that is a great trade; for write-heavy tables, think harder.

  9. Step 9: More indexes is not better

    Suppose someone adds 6 indexes "just in case". Now each insert writes 7 page writes, and every update to an indexed column pays too. Each index also takes space: at about 40 bytes an entry, an index on 10,000,000 rows is roughly 400 MB, held in memory and disk.

    Indexes nobody uses are pure cost. Add an index for a query you really run, check that it is used with EXPLAIN, and drop the ones that are not.

    Also do not index columns with few values (like a yes/no flag) on their own: they cannot narrow the search much.

  10. Step 10: Indexes in one picture

    Without an index, the database scans all 10,000,000 rows: 2,000 ms. A B-tree index finds one row in 4 page reads: 0.4 ms, and it also serves ranges and sorting.

    Composite indexes follow the left-prefix rule, and covering indexes skip the table. Every index costs space and slows each write, so index the queries you actually run.