Skip to content

Instantly share code, notes, and snippets.

@tommy-muehle
Created February 15, 2019 07:45
Show Gist options
  • Save tommy-muehle/e7c1ae397b614efb9ff29be5aab0bae6 to your computer and use it in GitHub Desktop.
Save tommy-muehle/e7c1ae397b614efb9ff29be5aab0bae6 to your computer and use it in GitHub Desktop.
PostgreSQL Index Usage Analysis
SELECT
relname AS TableName,
to_char(seq_scan, '999,999,999,999') AS TotalSeqScan,
to_char(idx_scan, '999,999,999,999') AS TotalIndexScan,
to_char(n_live_tup, '999,999,999,999') AS TableRows,
pg_size_pretty(pg_relation_size(relname :: regclass)) AS TableSize
FROM pg_stat_all_tables
WHERE schemaname = 'public'
AND 50 * seq_scan > idx_scan -- more then 2%
AND n_live_tup > 10000
AND pg_relation_size(relname :: regclass) > 5000000
ORDER BY relname ASC;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment