如何检查表在DB2中使用了多少空间

M.J*_*.J. 5 db2

DB2中是否有一种方法可以识别表正在消耗分配给基础表空间的总空间的多少。”

谢谢

小智 9

DB2 查询以检查表大小

select 
   a.CARD*(sum(b.AVGCOLLEN)+10)/1024/1024 as Tablesize_in_MB 
from 
   syscat.tables as a, syscat.columns as b 
where 
   a.TABNAME = b.TABNAME and b.TABNAME = 'TABLE_NAME' group by a.CARD
Run Code Online (Sandbox Code Playgroud)

数据大小

select 
  char(date(t.stats_time))||' '||char(time(t.stats_time)) as statstime 
  ,substr(t.tabschema,1,8)||'.'||substr(t.tabname,1,24) as tabname 
  ,card as rows_per_table 
  ,decimal(float(t.npages)/ ( 1024 / (b.pagesize/1024)),9,2) as used_mb 
  ,decimal(float(t.fpages)/ ( 1024 / (b.pagesize/1024)),9,2) as allocated_mb 
from 
  syscat.tables t , syscat.tablespaces b 
where t.tbspace=b.tbspace 
order by 5 desc with ur
Run Code Online (Sandbox Code Playgroud)

索引大小

select 
  rtrim(substr(i.tabschema,1,8))||'.'||rtrim(substr( i.tabname, 1,24)) as tabname 
 ,decimal(sum(i.nleaf)/( 1024 / (b.pagesize/1024)),12,2) as indx_used_per_table_mb 
from 
   syscat.indexes i, syscat.tables t , syscat.tablespaces b 
where 
   i.tabschema is not null and i.tabname=t.tabname 
   and i.tabschema=t.tabschema and t.tbspace=b.tbspace 
group by 
   i.tabname,i.tabschema, b.pagesize order by 2 desc with ur
Run Code Online (Sandbox Code Playgroud)


MrG*_*MrG 5

您的问题造成了错误的二分法,因为可能在不向表空间分配有限空间的情况下创建了表空间。限制可能是表空间所在的驱动器或共享。但是,如果您知道分配给表空间的空间,或者只需要表正在使用的表空间当前大小的百分比,那么可以的话,有一种方法可以知道。

SELECT * FROM SYScat.tables where tabname='mytable';
Run Code Online (Sandbox Code Playgroud)

会告诉您表格正在使用多少页。

然后在命令行:LiST TABLESPACES SHOW DETAIL 将告诉您表空间中的总页数以及页的大小(以字节为单位)。

Select * from sysibmadm.tbsp_utilization where tbsp_name='MyTblSpace' 
Run Code Online (Sandbox Code Playgroud)

如果有一个表空间,它将为您提供最大的表空间大小。

  • 该答案仅适用于在Linux,UNIX或Windows上运行的DB2。此外,仅在执行RUNSTATS时才更新syscat.tables中的NPAGES和FPAGES。这些值可能不准确。此外,它们不包括分配给索引,LOB等的空间。最好查看SYSIBMADM.ADMINTABINFO。 (2认同)