如何列出PostgreSQL数据库的所有表并按大小排序?
当前回答
我需要找出哪些表占用了最多的空间。
根据其他人的回答,我使用了这个问题:
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,n_live_tup, pg_size_pretty(pg_relation_size(table_name))
from information_schema.tables
inner join pg_stat_user_tables on table_name=relname
where table_schema = 'public'
order by 2 desc
另一种替代方法
我需要找出哪些表占用了最多的空间。
根据其他人的回答,我使用了这个问题:
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
我应该买个更大的固态硬盘。
这将显示模式名、表名、size pretty和size(排序所需)。
SELECT
schema_name,
relname,
pg_size_pretty(table_size) AS size,
table_size
FROM (
SELECT
pg_catalog.pg_namespace.nspname AS schema_name,
relname,
pg_relation_size(pg_catalog.pg_class.oid) AS table_size
FROM pg_catalog.pg_class
JOIN pg_catalog.pg_namespace ON relnamespace = pg_catalog.pg_namespace.oid
) t
WHERE schema_name NOT LIKE 'pg_%'
ORDER BY table_size DESC;
我基于PostgreSQL数据库中具有大小(相对大小和绝对大小)的模式列表的解决方案来构建它
如果你正在寻找一个总的分解,吐司和索引大小使用这个:
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
table_name,
pg_size_pretty( pg_total_relation_size(quote_ident(table_name))),
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)) DESC
你可以看到总大小在一个漂亮的格式,但它的顺序也是正确的。
推荐文章
- SQL Server -事务回滚错误?
- 如何在SQL中有效地计数列值的发生?
- PostgreSQL列名区分大小写吗?
- 在WHERE子句中引用列别名
- 恢复未保存的SQL查询脚本
- 如果任何字段包含NULL, MySQL CONCAT将返回NULL
- 向临时表中插入数据
- postgresql COUNT(DISTINCT…)非常慢
- 无法绑定多部分标识符
- 为什么Oracle 9i将空字符串视为NULL?
- Postgres:检查数组字段是否包含值?
- 导入CSV文件到SQL Server中
- 如何在postgresql中创建数据库用户?
- NVL和Coalesce之间的Oracle差异
- 在SQL server查询中将NULL替换为0