是否有一種簡單的方法可以列出PostgreSQL數據庫中的所有表並按大小排序?postgresql按大小排列列表和排序表
僞代碼
SELECT * FROM tables
ORDER by tables.size
我使用PostgreSQL
9.3.2
。
是否有一種簡單的方法可以列出PostgreSQL數據庫中的所有表並按大小排序?postgresql按大小排列列表和排序表
僞代碼
SELECT * FROM tables
ORDER by tables.size
我使用PostgreSQL
9.3.2
。
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
不工作:https://gist.github.com/maciejkowalski/e306cfb8e31a41308bdf –
它是table_schema,而不是schema_name。第一個查詢很好,但是您已經開始在您的psql會話中輸入一些內容,導致語法錯誤。 – yieldsfalsehood
@沒什麼特別 - 在這裏:對不起,在第二個查詢中輸入錯誤。 –
這將s你的架構名稱,表名,大小漂亮和大小(需要排序)。
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
也適用於+1,但@a_horse_with_no_name速度更快,所以我打算接受他的回答。 :) –
它實際上是基於來自@a_horse_with_no_name的帖子。我只是對我的要求表示讚賞。 – Kuchi
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 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
如果您爲解釋爲何建議的方法有幫助,您的答案將更有價值。 –
它與馬的答案類似,只是按照大小排序,因爲排序視圖很漂亮,所以很容易看。 – Spike
請使用「答覆」空間中的編輯鏈接將該文本添加到您的答案中。然後,您的貢獻將符合StackOverflow指南(在幫助中心閱讀一下):-) –
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
採取行psql cli那麼一個簡單的'\ d +'會告訴你這些信息,儘管沒有排序。 – cdhowie
謝謝。但我*需要它*排序,我有太多的表。 –
尋找相同的東西,但數據庫而不是表的人:[這裏是解決方案](https://stackoverflow.com/a/18907188/812102)。 –