// SQL snippet

Find the most-read tables in PostgreSQL

Find out which tables actually dominate read traffic before you spend effort optimizing anywhere else.

Main statement
PostgreSQL
Deckt außerdem ab
SQL Server

The task

Tuning effort should follow traffic. pg_stat_user_tables exposes both read paths: seq_tup_read for full scans and idx_tup_fetch for rows retrieved through indexes. Summing them gives total rows read per table; the write counters (n_tup_ins, n_tup_upd, n_tup_del) sit beside them so a table that is read-heavy AND write-heavy can be flagged for special care.

The snippet

Written for PostgreSQL. Versions for SQL Server are under Variations.

PostgreSQL
SELECT schemaname, relname AS table_name,
       seq_tup_read + COALESCE(idx_tup_fetch, 0) AS total_rows_read,
       seq_tup_read, idx_tup_fetch,
       n_tup_ins, n_tup_upd, n_tup_del,
       n_tup_hot_upd,
       pg_size_pretty(pg_total_relation_size(relid)) AS total_size
FROM pg_stat_user_tables
ORDER BY total_rows_read DESC
LIMIT 25;

So funktioniert es

The counters in pg_stat_user_tables accumulate since the last stats reset. idx_tup_fetch counts live rows fetched by index scans and seq_tup_read counts live rows returned by sequential scans. idx_tup_fetch is NULL for a table with no indexes, so COALESCE keeps those tables from sorting to the top with a NULL total. n_tup_hot_upd matters because a HOT (heap-only tuple) update writes no new index entries: when it is a small fraction of n_tup_upd on a busy table, every update is also writing to every index on it.

Variations

SQL Server: tables by index access and write counts

SQL Server
SELECT TOP 25
    OBJECT_SCHEMA_NAME(ios.object_id) AS schema_name,
    OBJECT_NAME(ios.object_id) AS table_name,
    SUM(ios.range_scan_count) AS range_scans,
    SUM(ios.singleton_lookup_count) AS singleton_lookups,
    SUM(ios.leaf_insert_count + ios.leaf_update_count + ios.leaf_delete_count) AS leaf_writes
FROM sys.dm_db_index_operational_stats(DB_ID(), NULL, NULL, NULL) ios
WHERE OBJECTPROPERTY(ios.object_id, 'IsUserTable') = 1
GROUP BY ios.object_id
ORDER BY SUM(ios.range_scan_count + ios.singleton_lookup_count) DESC;

Questions that come up

What is a good HOT update ratio?

It depends on the table. A HOT update needs two things: the update changes no indexed column, and the new row version fits on the same heap page. If the hot path updates an indexed column, such as an indexed updated_at, HOT is impossible for it. If no indexed column changes but the ratio is still low, the pages are full; a lower fillfactor on the table (for example 90) leaves room for new versions.

Do these counters survive restarts?

A clean shutdown and restart keeps them, because the server writes them to disk at shutdown. Crash recovery, failover to a standby and pg_stat_reset() clear them. Copy them into a table on a schedule if you want trend lines.

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.