// SQL snippet

List all tables with sizes

Before you reclaim space or archive data, you need to know which tables are actually big. This recipe lists every table in a schema with its total size, biggest first.

Main statement
PostgreSQL
다음도 지원
SQL Server MySQL SQLite

The task

The disk holding the database keeps filling and no one can say which table is responsible. Start with a ranked size list.

The snippet

Written for PostgreSQL. Versions for SQL Server, MySQL and SQLite are under Variations.

PostgreSQL
SELECT c.relname AS table_name,
       pg_total_relation_size(c.oid) AS total_bytes,
       pg_size_pretty(pg_total_relation_size(c.oid)) AS total_size
FROM pg_class c
JOIN pg_namespace n ON n.oid = c.relnamespace
WHERE c.relkind = 'r'
  AND n.nspname = 'public'
ORDER BY pg_total_relation_size(c.oid) DESC;

작동 방식

pg_total_relation_size counts the table plus its indexes, TOAST data and free-space map, which is the number that shows up in disk usage. The join to pg_namespace filters to one schema and relkind 'r' keeps ordinary tables while leaving views and partitioned parents out. Ordering by the raw byte count sorts biggest-first; the pretty column is only for display.

Variations

SQL Server

SQL Server
-- reserved pages across heap/clustered index, nonclustered
-- indexes and LOB data; rows counted from index 0 or 1 only
SELECT SCHEMA_NAME(t.schema_id) AS schema_name,
       t.name AS table_name,
       SUM(CASE WHEN ps.index_id IN (0, 1) THEN ps.row_count END) AS row_count,
       CAST(SUM(ps.reserved_page_count) * 8 / 1024.0 AS decimal(12,1)) AS size_mb
FROM sys.dm_db_partition_stats ps
JOIN sys.tables t ON t.object_id = ps.object_id
GROUP BY t.schema_id, t.name
ORDER BY SUM(ps.reserved_page_count) DESC;

MySQL

MySQL
SELECT table_name,
       ROUND((data_length + index_length) / 1024 / 1024, 1) AS size_mb,
       table_rows AS row_estimate
FROM information_schema.tables
WHERE table_schema = 'mydb' AND table_type = 'BASE TABLE'
ORDER BY data_length + index_length DESC;

SQLite

SQLite
-- needs a build with the dbstat virtual table (the sqlite3
-- shell has it); index b-trees are folded into their table
SELECT m.tbl_name AS table_name,
       SUM(s.pgsize) AS total_bytes
FROM dbstat s
JOIN sqlite_master m ON m.name = s.name
WHERE m.tbl_name NOT LIKE 'sqlite_%'
GROUP BY m.tbl_name
ORDER BY total_bytes DESC;

Questions that come up

Why doesn't the total match the data directory size?

Per-table sizes leave out WAL, system catalogs, temp files and other databases in the cluster. They do include dead rows: plain VACUUM marks that space reusable but rarely returns it to the OS, so a table that once held many more rows keeps its size until VACUUM FULL or pg_repack rewrites it.

Keep reading

// snippets are better with a schema brain

Run these with autocomplete that knows your tables

SQLly is a native SQL workbench for macOS, Windows and Linux. Paste a snippet into a live connection and you get schema-aware IntelliSense, a plan viewer and guards against unsafe edits.