← Back to SQL, page 19

sql-0362 · Medium mid

Insert throughput on a busy table dropped after the team added many indexes. How would you decide which to keep?

Every index must be updated on each insert and on updates of its columns, so extra indexes slow writes and use memory. Check index usage statistics, drop unused or duplicate indexes, merge overlapping ones into a composite index, and keep only those that serve real queries.

Full answer

Evidence to gather:

  • Usage counts: PostgreSQL's pg_stat_user_indexes.idx_scan shows indexes that are never used since the last stats reset (check replicas too, since reports may run there).
  • Duplicates: an index on (a) is redundant when (a, b) exists, for most queries.
  • Size: large indexes compete for cache with the table itself.

Decisions:

  • Drop unused and redundant indexes, ideally with DROP INDEX CONCURRENTLY.
  • Replace several single-column indexes with one composite index matching the main filter and sort.
  • Use partial indexes for queries on a small subset (WHERE status = 'pending').
  • Keep indexes that enforce uniqueness, even if rarely scanned.

Measure insert time before and after on a copy of production data. Bulk loads can drop and recreate secondary indexes around the load.

Key points

  • Each index costs writes and memory
  • Usage statistics show unused indexes
  • Composite indexes can replace several singles
  • Partial indexes target subsets
  • Keep uniqueness indexes

Common mistakes

  • Indexing every column 'just in case'
  • Dropping a unique index because it shows few scans

Follow-ups

  • Why might an index be used only on a replica?
  • How do partial indexes save space?

Tips

  • Ground the choice in usage data