2015-09-28 26 views
0

非常感謝您對此提供任何幫助。PSQL - 選擇分區和正常表的大小

所以,基本上,我有一個Greenplum數據庫,我希望爲前10個最大的表格選擇表格大小。這不是一個問題,使用下面的:

select 
sotaidschemaname schema_name 
,sotaidtablename table_name 
,pg_size_pretty(sotaidtablesize) table_size 
from gp_toolkit.gp_size_of_table_and_indexes_disk 
order by 3 desc 
limit 10 
; 

但是我有我的數據庫中的幾個分區表和這些顯示了上面的SQL爲所有的「子表」分成小片段(雖然我知道他們準備製作最大的2張桌子)。有沒有辦法選擇表(分區或其他)和他們的總大小的腳本?

注意:我很樂意包含某種連接,因爲我只指定了分區表名,因爲只有2個分區表。但是,我仍然需要進入前10名(我不能假定分區表在那裏),並且我不能指定任何其他表名,因爲其中有近一千個。

再次感謝, 溫尼。

回答

0

你的朋友會pg_relation_size()函數用於獲取關係的大小和你選擇的pg_class,pg_namespace和pg_partition像這樣一起加入他們:

select schemaname, 
     tablename, 
     sum(size_mb) as size_mb, 
     sum(num_partitions) as num_partitions 
    from (
     select coalesce(p.schemaname, n.nspname) as schemaname, 
       coalesce(p.tablename, c.relname) as tablename, 
       1 as num_partitions, 
       pg_relation_size(n.nspname || '.' || c.relname)/1000000. as size_mb 
      from pg_class as c 
       inner join pg_namespace as n on c.relnamespace = n.oid 
       left join pg_partitions as p on c.relname = p.partitiontablename and n.nspname = p.partitionschemaname  
     ) as q 
    group by 1, 2 
    order by 3 desc 
    limit 10; 
0
select * from 
(  
select schemaname,tablename, 
pg_relation_size(schemaname||'.'||tablename) as Size_In_Bytes 
from pg_tables 
where schemaname||'.'||tablename not in (select schemaname||'.'||partitiontablename from pg_partitions) 
and schemaname||'.'||tablename not in (select distinct schemaname||'.'||tablename from pg_partitions) 

union all 

select schemaname,tablename, 
sum(pg_relation_size(schemaname||'.'||partitiontablename)) as Size_In_Bytes 
from pg_partitions 
group by 1,2) as foo 

where Size_In_Bytes >= '0' order by 3 desc; 
相關問題