代码改变世界

如何计算看带Lob字段(CLOB、BLOB)的表的大小

2020-08-11 12:34  dba+  阅读(1415)  评论(0编辑  收藏  举报

  Oracle中,对表上的lob字段是单独存储的,所以使用segment_name = 表名 的时候,只能统计非 lob 字段的数据大小,无法统计上lob字段的。正确的计算方式应该是:dba_segments中表占用的空间+LOB segments占的空间+ LOB 的索引。
  正确的查询语句如下:

SELECT
 (SELECT SUM(S.BYTES)                                                                                                 -- The Table Segment size
  FROM DBA_SEGMENTS S
  WHERE S.OWNER = UPPER('&SCHEMA') AND
       (S.SEGMENT_NAME = UPPER('&TABNAME'))) +
 (SELECT SUM(S.BYTES)                                                                                                 -- The Lob Segment Size
  FROM DBA_SEGMENTS S, DBA_LOBS L
  WHERE S.OWNER = UPPER('&SCHEMA') AND
       (L.SEGMENT_NAME = S.SEGMENT_NAME AND L.TABLE_NAME = UPPER('&TABNAME') AND L.OWNER = UPPER('&SCHEMA'))) +
 (SELECT SUM(S.BYTES)                                                                                                 -- The Lob Index size
  FROM DBA_SEGMENTS S, DBA_INDEXES I
  WHERE S.OWNER = UPPER('&SCHEMA') AND
       (I.INDEX_NAME = S.SEGMENT_NAME AND I.TABLE_NAME = UPPER('&TABNAME') AND INDEX_TYPE = 'LOB' AND I.OWNER = UPPER('&SCHEMA')))
  "TOTAL TABLE SIZE"
FROM DUAL;


参考文章:
How to Compute the Size of a Table containing Outline CLOBs and BLOBs (文档 ID 118531.1)