PostgreSQLデータベースのすべてのテーブルをリストし、サイズ順に並べ替えるにはどうすればよいですか?
PostgreSQLデータベースのすべてのテーブルをリストし、サイズ順に並べ替えるにはどうすればよいですか?
回答:
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
マニュアルのすべてのオブジェクトサイズ関数のリスト。
select table_schema, table_name, pg_relation_size(table_schema||'.'||table_name) from information_schema.tables order by 3;
します。助けてくれてありがとう!
select * from information_schema.tables where table_schema = 'public';
あっても、行は生成されません\dn
。多分9.5の変更がこれを引き起こしましたか?
これにより、スキーマ名、テーブル名、サイズ、サイズ(並べ替えに必要)が表示されます。
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データベースのサイズ(相対および絶対)を持つスキーマのここのリストからのソリューションに基づいて構築します
これはより明確になります。
pg_size_pretty(<numeric_value>)
-no.ofバイトを人間が読める形式に変換します。
pg_database_size(<db_name>)
-データベースサイズをバイト単位で取得します。
pg_total_relation_size(<relation_name>)
-テーブルとそのインデックスの合計サイズをバイト単位で取得します。
pg_relation_size(<relation_name>)
-リレーション(テーブル/インデックス)サイズをバイト単位で取得します。
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
ヒト化フォーマットがで表しているbytes
、kB
、MB
、GB
、とTB
。
bytes
へkB
-始まる10240 bytes
bytes
MB
-から始まる10485248 bytes
= 10239.5 kB
〜10 MB
bytes
GB
-から始まる10736893952 bytes
= 10239.5 MB
〜10 BG
bytes
TB
-から始まる10994579406848 bytes
= 10239.5 GB
〜10 TB
すべての単位変換はから始まり10 + <unit>
ます。
参考のために-Postgres公式ドキュメント
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
最もスペースを使用しているテーブルを見つける必要がありました。
他の回答に基づいて、私はそのクエリを使用しました:
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を購入する必要がありました。
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
\d+
はソートされていませんが、この情報が表示されます。