Sub topic 12
What Indexes Cost on Writes and in Storage
Assume you know basic SELECT, INSERT, UPDATE, and DELETE, and you understand tables, rows, columns, and primary keys. This chapter uses PostgreSQL syntax.
Indexes make many reads faster, but they are not free. An index is another data structure that the database must store and maintain. Each index can increase write time, storage use, backup size, and maintenance work.
The cost of inserting a row
When you insert a row, the database writes the row to the table and adds an entry to every index whose columns apply to that row.
CREATE TABLE orders (
id bigint PRIMARY KEY,
customer_id bigint NOT NULL,
status text NOT NULL,
created_at timestamptz NOT NULL,
total_cents integer NOT NULL
);
CREATE INDEX orders_customer_id_idx
ON orders (customer_id);
CREATE INDEX orders_status_idx
ON orders (status);
INSERT INTO orders
(id, customer_id, status, created_at, total_cents)
VALUES
(1, 42, 'open', now(), 2599);This insert updates the table, the primary-key index, the customer index, and the status index. The database must also keep each index ordered and may need to split an index page when it becomes full.
An index page is a block of storage that holds index entries. Page splits can cause extra writes and can make an index use more space over time. This is called write amplification: one logical change causes several physical writes.
Updates and deletes
An update costs more when it changes an indexed column. The database usually removes the old index entry and adds a new one. Updating a non-indexed column can still create a new row version, depending on the database engine and storage conditions, but it does not normally require changing indexes that do not contain that column.
A delete must remove the row's entry from every relevant index. Some systems mark entries for later cleanup instead of immediately reclaiming their space. PostgreSQL uses vacuum to clean up obsolete row versions and index entries.
| Operation | Table work | Index work |
|---|---|---|
| Insert | Add a row | Add one entry to each applicable index |
| Update indexed column | Add a new row version | Remove or replace old entries and add new ones |
| Delete | Mark or remove a row | Remove or mark entries in each index |
Storage and maintenance
Indexes consume disk space. A wide index entry uses more space than a narrow one, and a composite index stores values for multiple columns. Including extra columns can help an index-only scan, but it makes the index larger.
More indexes also increase:
- Database and backup size.
- Memory pressure when indexes compete for cache space.
- Replication traffic caused by index updates.
- Time needed for vacuum, reindexing, and recovery.
The exact cost depends on row size, index type, write rate, and data distribution.
Choosing the trade-off
Create an index when it supports an important query, constraint, or ordering requirement. Avoid indexes that duplicate another index or support queries that are rarely run.
For each candidate index, ask:
- Which query or constraint needs it?
- How often does that query run?
- How much faster does it make the query?
- How often do indexed rows change?
- Can an existing index serve the same purpose?
Use workload measurements rather than assuming that every possible filter deserves an index. A small set of well-chosen indexes usually gives better results than indexing every column.
Exercise
A table receives many inserts and has four indexes. A new index would speed up one report that runs once per day. What should you investigate before creating it?
Show answer
Measure the report's current cost, the insert rate, and the new index's storage and write overhead. Create it only if the report's benefit justifies those ongoing costs.