如何列出PostgreSQL数据库的所有表并按大小排序?
当前回答
您可以获得总关系大小和关系大小,这可能取决于您的表关系。 下面是如何获取数据库中的前100个表:
SELECT schemaname AS table_schema,
relname AS table_name,
PG_SIZE_PRETTY(PG_TOTAL_RELATION_SIZE(relid)) AS total_size,
PG_SIZE_PRETTY(PG_RELATION_SIZE(relid)) AS data_size,
PG_SIZE_PRETTY(PG_TOTAL_RELATION_SIZE(relid) - PG_RELATION_SIZE(relid))
AS external_size
FROM pg_catalog.pg_statio_user_tables
ORDER BY PG_TOTAL_RELATION_SIZE(relid) DESC,
PG_RELATION_SIZE(relid) DESC
LIMIT 100;
其他回答
我需要找出哪些表占用了最多的空间。
根据其他人的回答,我使用了这个问题:
select table_name, pg_size_pretty( pg_relation_size(quote_ident(table_name)) )
from information_schema.tables
where table_schema = 'public'
order by pg_relation_size(quote_ident(table_name)) desc
得到如下结果:
table_name pg_size_pretty
--------------------------------------
trade_binance 96 GB
closs_v2_binance_stash 46 GB
closs_bitfinex_stash 5725 MB
trade_bitfinex 5112 MB
...
api_requests 0 bytes
trade_huobi 0 bytes
我应该买个更大的固态硬盘。
select table_name, pg_relation_size(quote_ident(table_name))
from information_schema.tables
where table_schema = 'public'
order by 2
如果你有多个模式,这将显示模式公共中所有表的大小,你可能想使用:
select table_schema, table_name, pg_relation_size('"'||table_schema||'"."'||table_name||'"')
from information_schema.tables
order by 3
SQLFiddle示例:http://sqlfiddle.com/#!15/13157/3
手册中所有对象大小函数的列表。
select table_name, pg_size_pretty(pg_total_relation_size(quote_ident(table_name)))
from information_schema.tables
where table_schema = 'public'
order by pg_total_relation_size(quote_ident(table_name));
Pg_total_relation_size将包括索引和表的大小。 如果只需要表大小,那么pg_relation_size就足够了。
如果你正在寻找一个总的分解,吐司和索引大小使用这个:
SELECT *, pg_size_pretty(total_bytes) AS total
, pg_size_pretty(index_bytes) AS INDEX
, pg_size_pretty(toast_bytes) AS toast
, pg_size_pretty(table_bytes) AS TABLE
FROM (
SELECT *, total_bytes-index_bytes-COALESCE(toast_bytes,0) AS table_bytes
FROM (
SELECT c.oid,nspname AS table_schema, relname AS TABLE_NAME
, c.reltuples AS row_estimate
, pg_total_relation_size(c.oid) AS total_bytes
, pg_indexes_size(c.oid) AS index_bytes
, pg_total_relation_size(reltoastrelid) AS toast_bytes
FROM pg_class c
LEFT JOIN pg_namespace n ON n.oid = c.relnamespace
WHERE relkind = 'r'
) a
) a ORDER BY total_bytes DESC;
select uv.a tablename, pg_size_pretty(uv.b) sizepretty
from (select tb.tablename a, pg_table_size('schemaname.'||tb.tablename::text) b
from pg_tables tb
where tb.schemaname ilike 'schemaname'
order by 2 desc
) uv
推荐文章
- Postgresql列表和排序表的大小
- 如何获得列中每个不同值的计数?
- 插入……ON重复键(什么都不做)
- MySQL,更好地插入NULL或空字符串?
- 允许docker容器连接到本地/主机postgres数据库
- 选择其他表中没有的行
- 在PostgreSQL中使用UTC当前时间作为默认值
- 创建表如果在SQL Server中不存在
- 优化PostgreSQL进行快速测试
- 如何获取SQL Server数据脚本?
- django test app error -在创建测试数据库时出现错误:创建数据库的权限被拒绝
- 在Android SQLite中处理日期的最佳方法
- 如何在MySQL表中移动列?
- 在SQL Server中查找重复的行
- 从DateTime中提取小时(SQL Server 2005)