2015-09-28 55 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; 
相关问题