Oracle 计算表占用空间大小

我们可以通过系统视图DBA_SEGMENTS、USER_SEGMETNS、DBA_TABLES来查看一个表所占空间的大小,如下所示:

SELECT SEGMENT_NAME              TABLE_NAME
      ,SUM(BLOCKS)               BLOCKS
      ,SUM(BYTES)/(1024*1024)    "TABLE_SIZE[MB]"
FROM USER_SEGMENTS
WHERE  SEGMENT_TYPE='TABLE'
   AND SEGMENT_NAME=&TABLE_NAME
GROUP BY SEGMENT_NAME;
 
 
SELECT SEGMENT_NAME              TABLE_NAME
      ,SUM(BLOCKS)               BLOCKS
      ,SUM(BYTES)/(1024*1024)    "TABLE_SIZE[MB]"
FROM DBA_SEGMENTS
WHERE  SEGMENT_TYPE='TABLE'
   AND SEGMENT_NAME=&TABLE_NAME
GROUP BY SEGMENT_NAME;

如果你通过系统视图DBA_TABELS、USER_TABLES统计、查看表所占的空间大小,那么由于统计信息过时或总是持续有DML操作(INSERTR、UPDATE、DELETE),那么有可能用下面SQL统计出的信息可能不准确。需要先收集一下表对象的统计信息。 另外,可能每个数据库的db_block_size有所不同,不一定是8192。所以注意修改.

ANALYZE TABLE OWNER.TABLE_NAME COMPUTE STATISTICS;
 
或
 
EXEC DBMS_STATS.GATHER_TABLE_STATS('OWNER','TABLE_NAME');
 
 
SELECT OWNER                    OWNER_NAME
      ,TABLE_NAME               TABLE_NAME
      ,TABLESPACE_NAME          TABLESPACE_NAME
      ,BLOCKS                   ACTUAL_BLOCKS
      ,BLOCKS + EMPTY_BLOCKS    TOTAL_BLOCKS
      ,(BLOCKS + EMPTY_BLOCKS) * 8192/(1024*1024)
                                "TABLE_SIZE[MB]"
FROM DBA_TABLES
WHERE OWNER=&OWNER
  AND TABLE_NAME=&TABLE_NAME

关于DBA_TABLES和DBA_SEGMENTS表中的block的区别,其实它们的关系如下所示:

      自动段空间管理(Automatic Segment Space Management ASSM)

              dba_segments.blocks = dba_tables.blocks + dba_tables.empty_blocks

      本地管理表空间(Locally Managed Tablespace )

              dba_segments.blocks = dba_tables.blocks + dba_tables.empty_blocks+ 1(SEGMENT HEADER BLOCK)

 

DBA_SEGMENTS.BLOCKS      holds the total number of blocks allocated to the table.

DBA_TABLES.BLOCKS         holds the total number of blocks allocated for data.

 

DBA_SEGMENTS中的blocks表示分配给表的存储空间,而DBA_TABLES中blocks表示表中数据实际占有的存储空间。所以这个是有细微差别的。所以你如果要查表的实际占用大小,那么就要使用DBA_TABLES来查询。查看数据库中占用存储空间从大到小的所有表对象

 

SELECT SEGMENT_NAME              TABLE_NAME
      ,SUM(BLOCKS)               BLOCKS
      ,SUM(BYTES)/(1024*1024)    "TABLE_SIZE[MB]"
FROM DBA_SEGMENTS
WHERE  SEGMENT_TYPE='TABLE'
GROUP BY SEGMENT_NAME
ORDER BY BLOCKS DESC;

 

转自:http://www.cnblogs.com/kerrycode/p/4280404.html

使用sys帐号统计某个用户下的所有表的大小:

SELECT * FROM
(
SELECT SEGMENT_NAME, SUM(BYTES)/1024/1024/1024 GB
FROM dba_SEGMENTS
WHERE OWNER = '用户名大写'
GROUP BY SEGMENT_NAME
ORDER BY 2 DESC
) b
WHERE b.segment_name IN (SELECT table_name FROM dba_tables WHERE  OWNER='用户名大写')

  

 

你可能感兴趣的:(Oracle 计算表占用空间大小)