Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions high_cardinality_columns.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
-- run "analyze" to analyze all tables
SELECT
schemaname,
tablename,
attname AS column,
n_distinct,
CASE WHEN n_distinct >= 0 THEN
n_distinct::text
ELSE
('~' || (- n_distinct * rel.reltuples)) -- negative means "estimated fraction of table"
END AS estimated_distinct,
rel.reltuples::numeric AS estimated_rows
FROM
pg_stats s
JOIN pg_class rel ON rel.relname = s.tablename
JOIN pg_namespace nsp ON nsp.oid = rel.relnamespace
AND nsp.nspname = s.schemaname
WHERE
n_distinct IS NOT NULL
AND schemaname NOT IN ('pg_catalog','information_schema')
ORDER BY
ABS(n_distinct) DESC
LIMIT 10;