Finding missing indexes with PostgreSQL

in #postgresql6 years ago (edited)

1200px-Postgresql_elephant.svg.png

This is a rather quick post, where I will guide myself through finding columns to index on a non-performant PostgreSQL installation.
SELECT

  relname,

  seq_scan - idx_scan AS too_much_seq,

  CASE

    WHEN

      seq_scan - coalesce(idx_scan, 0) > 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;

Coin Marketplace

STEEM 0.20
TRX 0.15
JST 0.029
BTC 62864.56
ETH 2538.87
USDT 1.00
SBD 2.93