// SQL snippet

Compute index usage ratio per table in PostgreSQL

One number per table: what share of reads used an index. Low ratios on big tables are your next index project.

Main statement
PostgreSQL

The task

A table-level index percentage makes drift visible. If a large table served 90 percent of reads through indexes last month and only 60 percent this month, new queries or new data distribution are pushing work into full scans. The absolute access counts sit next to the ratio so tiny tables do not produce misleading percentages.

The snippet

Written for PostgreSQL.

PostgreSQL
SELECT relname AS table_name,
       seq_scan, idx_scan,
       seq_tup_read, idx_tup_fetch,
       ROUND(100.0 * COALESCE(idx_scan, 0) / NULLIF(COALESCE(idx_scan, 0) + seq_scan, 0), 1) AS index_pct
FROM pg_stat_user_tables
WHERE COALESCE(idx_scan, 0) + seq_scan > 100
ORDER BY COALESCE(idx_scan, 0) + seq_scan DESC;

Fonctionnement

The ratio divides idx_scan by all scans (index plus sequential) recorded in pg_stat_user_tables. idx_scan is NULL on a table with no indexes at all, and COALESCE turns that into 0 so those tables show 0 percent instead of disappearing from the list. The WHERE clause drops tables with too few scans for a percentage to mean anything. Read the ratio next to the row counts: a table at 99 percent index scans can still spend most of its time in the 1 percent if each of those sequential scans reads millions of rows.

Variations

Flag big tables with a low index percentage

SELECT relname AS table_name,
       seq_scan, idx_scan,
       ROUND(100.0 * COALESCE(idx_scan, 0) / NULLIF(COALESCE(idx_scan, 0) + seq_scan, 0), 1) AS index_pct,
       pg_size_pretty(pg_relation_size(relid)) AS table_size
FROM pg_stat_user_tables
WHERE COALESCE(idx_scan, 0) + seq_scan > 100
  AND pg_relation_size(relid) > 100 * 1024 * 1024
  AND COALESCE(idx_scan, 0) < seq_scan
ORDER BY pg_relation_size(relid) DESC;

Questions that come up

What ratio should a big OLTP table have?

A large table serving point lookups usually sits well above 95 percent. Tables whose queries read most of their rows (reporting tables, ETL staging) will scan and should. Compare each table against its own history rather than a universal target; a drop from 99 to 80 percent after a release is the signal.

The planner picks seq scans even though an index exists. Why?

Often the planner is right: when a query matches a large fraction of the rows, one sequential pass is cheaper than many random index probes. It can also be stale statistics (run ANALYZE and check again), a predicate that wraps the column in a function, or a type mismatch between the column and the parameter. EXPLAIN (ANALYZE, BUFFERS) on the query shows the estimated versus actual row counts, which tells you which case it is.

Keep reading

// snippets are better with a schema brain

Run these with autocomplete that knows your tables

SQLly is a native SQL workbench for macOS, Windows and Linux. Paste a snippet into a live connection and you get schema-aware IntelliSense, a plan viewer and guards against unsafe edits.