LESSON 05 · Index costs

Pay for every index on every write

An index is a second data structure that speeds some reads and must be kept up to date on writes.

SYSTEM VIEW

The write path

Diagram view
Rows read—
Page changes—

Watch the database engine find customer 7’s orders by a scan, then by an index, then count what six indexes cost every insert.

  1. A page asks for customer 7’s orders. The Orders table finds rows by primary key, not by customer, so the Database engine scans every row: 4 rows read to return 2. The scan grows with the table, however few rows match.

    Query
    customer_id = 7
    Read
    4 rows for 2 orders

    What changed

    • A scan of every row
    • 2 rows match customer 7
  2. A secondary index is a separate sorted structure. The Customer index sorts its keys by customer_id, then date, so customer 7’s 2 entries sit side by side: the engine reads just those 2 entries, and no row yet.

    Index on
    (customer_id, created_at)
    Entries read
    2

    What changed

    • Customer index, sorted
    • 2 entries read for customer 7
  3. Each entry points to its row, so the engine fetches customer 7’s 2 rows from the Orders table, one at each end. In Postgres an entry holds the row’s location; in InnoDB it holds the primary key, so each fetch walks a second tree.

    Postgres
    entry → row’s location
    InnoDB
    entry → primary key

    What changed

    • 2 row fetches
    • InnoDB: a second tree walk each
  4. The keys sort by customer_id first, so the index serves one customer’s orders by date. One date’s orders sit apart, one per customer: Tuesday’s 2 entries lie at either end. So it doesn’t serve all customers’ orders on one date.

    Serves
    one customer, by date
    Not
    one date, every customer

    What changed

    • Customer 7’s entries together
    • Tuesday’s entries apart
  5. The order page lists a customer’s 20 newest orders with totals. The Customer index now stores total too, so it answers alone: no row fetched, up to 20 fetches avoided. The price is a bigger index that changes whenever total does.

    Page of 20
    up to 20 fetches saved
    Price
    bigger, changes with total

    What changed

    • total stored in each entry
    • 2 row fetches
    • A bigger index
  6. Customer 7 orders again. The engine adds a row to the Orders table and an entry to each of 6 secondary indexes: 7 page changes, each logged. At 2,000 inserts a second, that is about 14,000 page changes a second.

    Inserts
    2,000/s
    Each insert
    1 row + 6 entries

    What changed

    • 5 more indexes
    • 7 Thu: a row and 6 entries
    • 14,000 page changes/s
  7. Usage counts in pg_stat_user_indexes show 3 indexes nothing scans, so they go: each insert now changes 4 pages, about 8,000 a second. Index for queries you have measured, and skip indexes on tiny tables and for queries that read most rows anyway.

    Usage
    pg_stat_user_indexes
    Each insert
    1 row + 3 entries
    Also costs
    disk space, memory

    What changed

    • 3 unused indexes
    • 6,000 page changes/s

Space plays · ← → step · Home resets

THE IDEA

How it works

A primary index finds a row by its key. To find rows by something else, such as all orders for one customer, the engine must scan the table unless you add a secondary index: a separate sorted structure from that column’s value to the matching rows. In Postgres an entry points at the row’s physical location; in InnoDB it holds the primary key, so a lookup then walks a second tree. Column order matters in a composite index. An index on (customer_id, created_at) serves one customer’s orders by date, but not all customers’ orders on one date.

A covering index goes further and holds every column a query needs, so the engine can answer from the index without fetching rows. If a page shows a customer’s 20 newest orders with their totals, an index on (customer_id, created_at) that also stores total can avoid up to 20 fetches from scattered pages. The price is a bigger index, and more updates, since changing total now touches it too.

An insert must change the table and every index. As an example, 2,000 inserts per second into a table with 6 secondary indexes means about 14,000 page changes per second, each logged. Dropping 3 unused indexes cuts that to 8,000. Indexes also take disk space and compete for memory with the data they describe. So index for queries you have measured, check usage counts (Postgres reports them in pg_stat_user_indexes), and skip indexes on tiny tables and for queries that read most rows anyway.

DECISION POINT

An orders table has 9 indexes, and the nightly bulk import no longer fits its 6-hour window. Usage stats show that 4 indexes serve regular queries, and the other 5 show almost no scans over the last 30 days. What do you do?

Write a sentence or two to unlock the guidance. Committing first is what makes it stick.

Keep in mind

  • Every index speeds some reads and adds work to every write that touches its columns.
  • A covering index trades storage and write cost for skipping the row fetch.
  • Index for measured queries, and drop the ones nothing uses.
CHECK YOUR UNDERSTANDING

Put it into practice.

1 OF 3
ApplyQUESTION 01

SELECT status FROM orders WHERE customer_id = ? is slow because each match fetches a row from a different page. Which single index removes those fetches?

Choose an answer. You’ll see the explanation once you get it right.

Next lesson

0 of 3 checkpoints answered correctly to complete this lesson.