postgresql列表和按大小排序的表

时间:2014-02-12 20:02:59

标签: sql postgresql postgresql-9.3

是否有一种简单的方法可以列出PostgreSQL数据库中的所有表并按大小排序?

伪代码

SELECT * FROM tables
ORDER by tables.size

我正在使用PostgreSQL 9.3.2

10 个答案:

答案 0 :(得分:87)

select table_name, pg_relation_size(quote_ident(table_name))
from information_schema.tables
where table_schema = 'public'
order by 2

如果你有多个模式,这可能会显示模式public中所有表的大小,你可能想要使用:

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

手册中所有对象大小功能的列表:
https://www.postgresql.org/docs/current/static/functions-admin.html#FUNCTIONS-ADMIN-DBSIZE

答案 1 :(得分:51)

这将显示模式名称,表名称,大小漂亮和大小(排序所需)。

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;

我是根据此处list of schema with sizes (relative and absolute) in a PostgreSQL database

的解决方案构建的

答案 2 :(得分:9)

这将更加清楚。

pg_size_pretty(<numeric_value>)-将字节数转换为可读格式。

pg_database_size(<db_name>)-获取数据库大小,单位为 bytes

pg_total_relation_size(<relation_name>)-获取表及其索引的总大小,单位为 bytes

pg_relation_size(<relation_name>)-获取关系大小,单位为 bytes

pg_index_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

人性化格式以byteskBMBGBTB表示。

byteskB-从10240 bytes

开始

bytesMB-从10485248 bytes = 10239.5 kB10 MB

bytesGB-从10736893952 bytes = 10239.5 MB10 BG

bytesTB-从10994579406848 bytes = 10239.5 GB10 TB

所有单位转换均始于10 + <unit>

供参考-Postgres Official Documentation

答案 3 :(得分:8)

SELECT
   relname as "Table",
   pg_size_pretty(pg_total_relation_size(relid)) As "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;

取自https://wiki-bsse.ethz.ch/display/ITDOC/Check+size+of+tables+and+objects+in+PostgreSQL+database

答案 4 :(得分:2)

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

另一种选择

答案 5 :(得分:1)

我喜欢以下声明:

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

您可以以漂亮的格式查看总大小,但ID的顺序也正确。

答案 6 :(得分:1)

SELECT nspname || '.' || relname AS "relation",
    pg_size_pretty(pg_total_relation_size(C.oid)) AS "total_size"
  FROM pg_class C
  LEFT JOIN pg_namespace N ON (N.oid = C.relnamespace)
  WHERE nspname NOT IN ('pg_catalog', 'information_schema')
    AND C.relkind <> 'i'
    AND nspname !~ '^pg_toast'
  ORDER BY pg_total_relation_size(C.oid) DESC
  ;

信用: https://makandracards.com/makandra/52141-postgresql-how-to-show-table-sizes

答案 7 :(得分:0)

 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

答案 8 :(得分:0)

我需要找出哪些表使用最多的空间。

根据其他答案,我使用了该查询:

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

我应该买一个更大的SSD。

答案 9 :(得分:0)

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就足够了。