Last active
December 12, 2024 10:23
-
-
Save habedi/f93ee3de6622acc366fc0ff06b61b279 to your computer and use it in GitHub Desktop.
Get the information about the amount of storage used for tables (works for #mysql, #mariadb, #postgres)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Note that numbers are estimates, so the exact number of rows or index size might slightly differ from the number shown | |
-- For MySQL or MariaDB | |
select | |
table_name as `table name`, | |
table_rows as `row count`, | |
round(data_length / (1024 * 1024 * 1024), 2) as `data size (gb)`, | |
round(index_length / (1024 * 1024 * 1024), 2) as `index size (gb)`, | |
round((data_length + index_length) / (1024 * 1024 * 1024), 2) as `total size (gb)` | |
from | |
information_schema.tables | |
where | |
-- table_schema = coalesce(:schema_name, 'services') -- Doesn't work in `mysql` | |
table_schema = 'services' | |
order by | |
`total size (gb)` desc; | |
-------------------------------------------------------------------------------------------------------------------- | |
-- For PostgreSQL | |
select | |
c.relname as "table name", | |
coalesce(nullif(c.reltuples, -1), 0)::bigint as "row count", | |
round(pg_table_size(c.oid) / (1024.0 * 1024 * 1024), 2) as "data size (gb)", | |
round(pg_indexes_size(c.oid) / (1024.0 * 1024 * 1024), 2) as "index size (gb)", | |
round(pg_total_relation_size(c.oid) / (1024.0 * 1024 * 1024), 2) as "total size (gb)" | |
from | |
pg_class c | |
join | |
pg_namespace n on n.oid = c.relnamespace | |
where | |
-- n.nspname = coalesce(:schema_name, 'services') -- Doesn't work in `psql` | |
n.nspname = 'services' -- replace with your schema name | |
and c.relkind = 'r' -- only regular tables | |
order by | |
"total size (gb)" desc; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment