Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. with
  2. all_tables as
  3. (
  4. SELECT  *
  5. FROM    (
  6.     SELECT  'all'::text as table_name,
  7.         sum( (coalesce(heap_blks_read,0) + coalesce(idx_blks_read,0) + coalesce(toast_blks_read,0) + coalesce(tidx_blks_read,0)) ) as from_disk,
  8.         sum( (coalesce(heap_blks_hit,0)  + coalesce(idx_blks_hit,0)  + coalesce(toast_blks_hit,0)  + coalesce(tidx_blks_hit,0))  ) as from_cache    
  9.     FROM    pg_statio_all_tables  --> change to pg_statio_USER_tables if you want to check only user tables (excluding postgres's own tables)
  10.     ) a
  11. WHERE   (from_disk + from_cache) > 0 -- discard tables without hits
  12. ),
  13. tables as
  14. (
  15. SELECT  *
  16. FROM    (
  17.     SELECT  relname as table_name,
  18.         ( (coalesce(heap_blks_read,0) + coalesce(idx_blks_read,0) + coalesce(toast_blks_read,0) + coalesce(tidx_blks_read,0)) ) as from_disk,
  19.         ( (coalesce(heap_blks_hit,0)  + coalesce(idx_blks_hit,0)  + coalesce(toast_blks_hit,0)  + coalesce(tidx_blks_hit,0))  ) as from_cache    
  20.     FROM    pg_statio_all_tables --> change to pg_statio_USER_tables if you want to check only user tables (excluding postgres's own tables)
  21.     ) a
  22. WHERE   (from_disk + from_cache) > 0 -- discard tables without hits
  23. )
  24. SELECT  table_name as "table name",
  25.     from_disk as "disk hits",
  26.     round((from_disk::numeric / (from_disk + from_cache)::numeric)*100.0,2) as "% disk hits",
  27.     round((from_cache::numeric / (from_disk + from_cache)::numeric)*100.0,2) as "% cache hits",
  28.     (from_disk + from_cache) as "total hits"
  29. FROM    (SELECT * FROM all_tables UNION ALL SELECT * FROM tables) a
  30. ORDER   BY (case when table_name = 'all' then 0 else 1 end), from_disk desc;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement