如何列出PostgreSQL数据库的所有表并按大小排序?
当前回答
这样会更清楚。
Pg_size_pretty (<numeric_value>) -转换no。字节转换为人类可读的格式。
Pg_database_size (<db_name>) -以字节为单位获取数据库大小。
Pg_total_relation_size (<relation_name>) -以字节为单位获取表的总大小及其索引。
Pg_relation_size (<relation_name>) -以字节为单位获取关系(表/索引)大小。
Pg_indexes_size (<relation_name>) -以字节为单位获取关系的索引大小。
Current_database() -获取当前正在对其执行查询的数据库。
查询:
select current_database() as database,
pg_size_pretty(total_database_size) as total_database_size,
schema_name,
table_name,
pg_size_pretty(total_table_size) as total_table_size,
pg_size_pretty(table_size) as table_size,
pg_size_pretty(index_size) as index_size
from ( select table_name,
table_schema as schema_name,
pg_database_size(current_database()) as total_database_size,
pg_total_relation_size(table_name) as total_table_size,
pg_relation_size(table_name) as table_size,
pg_indexes_size(table_name) as index_size
from information_schema.tables
where table_schema=current_schema() and table_name like 'table_%'
order by total_table_size
) as sizes;
结果:
database | total_database_size | schema_name | table_name | total_table_size | table_size | index_size
-----------+---------------------+-------------+------------+------------------+------------+------------
vigneshdb | 1586 MB | corpdata | table_aaa | 16 kB | 0 bytes | 8192 bytes
vigneshdb | 1586 MB | corpdata | table_bbb | 24 kB | 0 bytes | 16 kB
vigneshdb | 1586 MB | corpdata | table_ccc | 640 kB | 112 kB | 488 kB
vigneshdb | 1586 MB | corpdata | table_ddd | 9760 kB | 3152 kB | 6568 kB
vigneshdb | 1586 MB | corpdata | table_eee | 1120 MB | 311 MB | 808 MB
人性化的格式以字节、kB、MB、GB和TB表示。
bytes to kB -从10240字节开始
bytes to MB -从10485248 bytes = 10239.5 kB ~ 10mb开始
bytes to GB -从10736893952开始,字节= 10239.5 MB ~ 10bg
bytes to TB -从10994579406848 bytes = 10239.5 GB ~ 10tb开始
所有单位转换从10 + <单位>开始。
参考- Postgres官方文件
其他回答
这将显示模式名、表名、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数据库中具有大小(相对大小和绝对大小)的模式列表的解决方案来构建它
您可以获得总关系大小和关系大小,这可能取决于您的表关系。 下面是如何获取数据库中的前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_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就足够了。
这样会更清楚。
Pg_size_pretty (<numeric_value>) -转换no。字节转换为人类可读的格式。
Pg_database_size (<db_name>) -以字节为单位获取数据库大小。
Pg_total_relation_size (<relation_name>) -以字节为单位获取表的总大小及其索引。
Pg_relation_size (<relation_name>) -以字节为单位获取关系(表/索引)大小。
Pg_indexes_size (<relation_name>) -以字节为单位获取关系的索引大小。
Current_database() -获取当前正在对其执行查询的数据库。
查询:
select current_database() as database,
pg_size_pretty(total_database_size) as total_database_size,
schema_name,
table_name,
pg_size_pretty(total_table_size) as total_table_size,
pg_size_pretty(table_size) as table_size,
pg_size_pretty(index_size) as index_size
from ( select table_name,
table_schema as schema_name,
pg_database_size(current_database()) as total_database_size,
pg_total_relation_size(table_name) as total_table_size,
pg_relation_size(table_name) as table_size,
pg_indexes_size(table_name) as index_size
from information_schema.tables
where table_schema=current_schema() and table_name like 'table_%'
order by total_table_size
) as sizes;
结果:
database | total_database_size | schema_name | table_name | total_table_size | table_size | index_size
-----------+---------------------+-------------+------------+------------------+------------+------------
vigneshdb | 1586 MB | corpdata | table_aaa | 16 kB | 0 bytes | 8192 bytes
vigneshdb | 1586 MB | corpdata | table_bbb | 24 kB | 0 bytes | 16 kB
vigneshdb | 1586 MB | corpdata | table_ccc | 640 kB | 112 kB | 488 kB
vigneshdb | 1586 MB | corpdata | table_ddd | 9760 kB | 3152 kB | 6568 kB
vigneshdb | 1586 MB | corpdata | table_eee | 1120 MB | 311 MB | 808 MB
人性化的格式以字节、kB、MB、GB和TB表示。
bytes to kB -从10240字节开始
bytes to MB -从10485248 bytes = 10239.5 kB ~ 10mb开始
bytes to GB -从10736893952开始,字节= 10239.5 MB ~ 10bg
bytes to TB -从10994579406848 bytes = 10239.5 GB ~ 10tb开始
所有单位转换从10 + <单位>开始。
参考- Postgres官方文件
我需要找出哪些表占用了最多的空间。
根据其他人的回答,我使用了这个问题:
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
我应该买个更大的固态硬盘。
推荐文章
- 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