Skip to content

Instantly share code, notes, and snippets.

@adamantnz
Created February 19, 2016 10:22
Show Gist options
  • Save adamantnz/8a4fe014b705323d3efd to your computer and use it in GitHub Desktop.
Save adamantnz/8a4fe014b705323d3efd to your computer and use it in GitHub Desktop.
Redshift - Table rows by schema
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
WHERE trim(pgn.nspname) = 'sat'
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