Skip to content

Instantly share code, notes, and snippets.

@cihangir
Created July 30, 2014 04:12
Show Gist options
  • Save cihangir/84699dfccd8bbc308528 to your computer and use it in GitHub Desktop.
Save cihangir/84699dfccd8bbc308528 to your computer and use it in GitHub Desktop.
Find missing indexes on tables
SELECT
relname,
seq_scan - idx_scan AS too_much_seq,
CASE
WHEN seq_scan - idx_scan > 0 THEN
'Missing Index?'
ELSE
'OK'
END,
pg_relation_size (relname :: regclass) AS rel_size,
seq_scan,
idx_scan
FROM
pg_stat_all_tables
WHERE
schemaname = 'public'
AND pg_relation_size (relname :: regclass) > 80000
ORDER BY
too_much_seq DESC;
This checks if there are more sequence scans then index scans.
If the table is small(<80000), it gets ignored,
since postgres seems to prefere sequence scans for them.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment