Index Overhead

Difficulty: Beginner

Question

Why shouldn't you add an index on every column of a table? What does each index cost?

Answer

Every index is a separate data structure that the database must update on every INSERT, every DELETE, and every UPDATE that touches an indexed column. Indexing every column multiplies write work, consumes disk and memory, and often does not even help reads, because the planner usually uses one well-chosen composite index rather than many single-column ones. Index the columns your real queries filter, join and sort on, and nothing more.

The costs of each index

  • Slower writes. Inserting one row into a table with 10 indexes means 1 heap write plus 10 index insertions, each possibly causing page splits. That write amplification also increases WAL or redo log volume and replication traffic.
  • Lost update optimisations. In PostgreSQL, a HOT (heap-only tuple) update avoids touching indexes when no indexed column changes and the page has space. Index a frequently updated column such as last_seen_at and you disable HOT for those updates.
  • Storage. Indexes can easily take as much space as the table itself. More storage also means bigger backups.
  • Memory pressure. Indexes compete for the buffer pool (shared_buffers or the InnoDB buffer pool). Rarely used indexes evict useful pages.
  • Maintenance. Vacuum, ANALYZE, rebuilds and schema migrations all take longer.
  • Planner overhead. More candidate indexes means more plans to evaluate, and occasionally a worse choice.

Why indexing every column does not help reads much

  • A query WHERE city = 'Pune' AND status = 'PAID' ORDER BY created_at is best served by one composite index (city, status, created_at). Three single-column indexes are much weaker; the planner may combine them with a bitmap AND, but cannot use them to avoid the sort.
  • Low-selectivity columns such as is_active or gender rarely benefit, because the planner prefers a sequential scan when a value matches a large share of rows. A partial index like WHERE is_active is better if needed.
  • Functions defeat plain indexes. WHERE lower(email) = ... needs an expression index, not just an index on email.

How to decide what to index

  1. Start from real queries: slow query logs, pg_stat_statements, or MySQL's performance schema.
  2. Run EXPLAIN ANALYZE and look for sequential scans on large tables, sorts and expensive joins.
  3. Design composite indexes for the most important queries, equality columns first, then range or sort columns.
  4. Index foreign key columns used in joins and cascades. PostgreSQL does not create these automatically; MySQL InnoDB does.
  5. Periodically find and drop unused or duplicate indexes.

Read-heavy vs write-heavy tables

Table typeIndex strategy
Product catalogue, read-heavyMore indexes acceptable for varied filters
Event log or payments ledger, insert-heavyMinimal indexes, often only the primary key and a time column
Hot rows updated constantlyAvoid indexing frequently changing columns
AnalyticsConsider a column store instead of many B-tree indexes
What one INSERT really writes. Each extra index adds work to every write.
  1. INSERT one order: Application sends a single row.
  2. Write table row: Heap page or clustered index updated.
  3. Update each index: One B-tree insertion per index.
  4. Write WAL or redo log: Every change is logged for durability.
  5. Ship to replicas: Replicas repeat the same index work.

Code examples

Finding unused indexes in PostgreSQL

-- Indexes never used since stats were last reset
SELECT s.relname        AS table_name,
       s.indexrelname   AS index_name,
       pg_size_pretty(pg_relation_size(s.indexrelid)) AS index_size,
       s.idx_scan
FROM pg_stat_user_indexes s
JOIN pg_index i ON i.indexrelid = s.indexrelid
WHERE s.idx_scan = 0
  AND NOT i.indisunique          -- keep indexes that enforce constraints
ORDER BY pg_relation_size(s.indexrelid) DESC;

-- Drop without blocking writes
DROP INDEX CONCURRENTLY IF EXISTS idx_orders_status;

Check the result on the primary and every replica, since stats are per server and a replica may serve the queries that use the index.

One composite index instead of three

-- Weak: separate indexes
-- CREATE INDEX ON orders (city);
-- CREATE INDEX ON orders (status);
-- CREATE INDEX ON orders (created_at);

-- Strong: matches the query shape, avoids a sort
CREATE INDEX idx_orders_city_status_created
  ON orders (city, status, created_at DESC);

SELECT id, amount_paise FROM orders
WHERE city = 'Pune' AND status = 'PAID'
ORDER BY created_at DESC LIMIT 20;

Equality columns first, then the sort column. One index serves the filter and the ordering, with a third of the write cost.

Key points

  • Every index adds work to inserts, deletes and updates of indexed columns
  • Indexes cost disk, buffer pool memory, WAL volume and replication bandwidth
  • Indexing a frequently updated column disables PostgreSQL HOT updates for it
  • One composite index matching the query usually beats several single-column ones
  • Use pg_stat_user_indexes or sys.schema_unused_indexes to find and drop unused indexes

Common follow-up questions

  • How do you choose column order in a composite index?
  • What is a covering index and an index-only scan?
  • What is a partial index and when would you use one?
  • How would you add an index to a 500 GB production table without downtime?

Interview tips

  • List several costs beyond 'slower writes', such as memory, WAL and replication
  • Say you index based on actual query patterns and EXPLAIN output, not guesses
  • Mention removing unused indexes; good engineers prune as well as add

Concepts covered

Index Maintenance, Write Amplification, HOT Updates, Unused Indexes, Query Planner

More Database Design questions