Skip to content

Instantly share code, notes, and snippets.

@samklr
Forked from kerbelp/redshift_table_usage.sql
Created June 28, 2018 09:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samklr/aee37ee1295b58704871e382f4f94a5e to your computer and use it in GitHub Desktop.
Save samklr/aee37ee1295b58704871e382f4f94a5e to your computer and use it in GitHub Desktop.
SELECT TRIM(pgdb.datname) AS DATABASE,
TRIM(pgn.nspname) AS SCHEMA,
TRIM(a.name) AS TABLE,
b.mbytes,
a.rows
FROM (SELECT db_id,
id,
name,
SUM(ROWS) AS ROWS
FROM stv_tbl_perm a
GROUP BY db_id,
id,
name) AS a
JOIN pg_class AS pgc ON pgc.oid = a.id
JOIN pg_namespace AS pgn ON pgn.oid = pgc.relnamespace
JOIN pg_database AS pgdb ON pgdb.oid = a.db_id
JOIN (SELECT tbl, COUNT(*) AS mbytes FROM stv_blocklist GROUP BY tbl) b ON a.id = b.tbl
ORDER BY mbytes DESC,
a.db_id,
a.name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment