数据表统计大小及容量计算

1. 有两种含义的表大小。一种是分配给一个表的物理空间数量,而不管空间是否被使用。可以这样查询获得字节数:

方法一:
select t.owner,t.segment_name,t.tablespace_name,sum(bytes/1024/1024/1024) gb
from dba_segments t
where t.tablespace_name='&TBS_NAME'
group by t.owner,t.segment_name,t.tablespace_name
order by 4 desc;

或者
   Select Segment_Name,Sum(bytes)/1024/1024 From User_Extents Group By Segment_Name

方法二:

select *
  from (select t.owner,
               t.segment_name,
               round(table_bytes / 1024 / 1024 / 1024, 0) TAB_GB,
               round(index_bytes / 1024 / 1024 / 1024, 0) IDX_GB
          from (select OWNER, SEGMENT_NAME, bytes table_bytes
                  from dba_SEGMENTS
                 where segment_type = 'TABLE') t,
               (select ui.table_name, sum(us.bytes) index_bytes
                  from dba_indexes ui, dba_segments us
                 where ui.index_name = us.segment_name
                   and us.segment_type = 'INDEX'
                 group by ui.table_name) i
         where t.segment_name = i.table_name(+)
         order by TAB_GB desc)
where rownum < 200;

 

方法三

另一种表实际使用的空间。这样查询:

analyze table emp compute statistics; 
select num_rows * avg_row_len 
from user_tables 
where table_name = 'EMP';



2. 查看每个表空间的大小
Select Tablespace_Name,Sum(bytes)/1024/1024 From Dba_Segments Group By Tablespace_Name

 

3. 通过block查询

-- Wasted space
select
   table_name,
   round(blocks*8/1024,2) as "size (MB)",
   round(num_rows*avg_row_len/1024/1024,2) as "actual_data (MB)",
   round(blocks*8/1024,2) - round(num_rows*avg_row_len/1024/1024,2) as "wasted_space (MB)"
from sys.user_tables
where round(blocks*8/1024,2) > round(num_rows*avg_row_len/1024/1024,2)
order by 4 desc
;

 

1.查询oracle表空间的使用情况

 select b.file_id  文件ID,
  b.tablespace_name  表空间,
  b.file_name     物理文件名,
  b.bytes       总字节数,
  (b.bytes-sum(nvl(a.bytes,0)))   已使用,
  sum(nvl(a.bytes,0))        剩余,
  sum(nvl(a.bytes,0))/(b.bytes)*100 剩余百分比
  from dba_free_space a,dba_data_files b
  where a.file_id=b.file_id
  group by b.tablespace_name,b.file_name,b.file_id,b.bytes
  order by b.tablespace_name

 

2.查询oracle系统用户的默认表空间和临时表空间

select default_tablespace,temporary_tablespace from dba_users

 

 3.查询单张表的使用情况

select segment_name,bytes from dba_segments where segment_name = 'RE_STDEVT_FACT_DAY' and owner = USER

RE_STDEVT_FACT_DAY是您要查询的表名称

 

4.查询所有用户表使用大小的前三十名

select * from (select segment_name,bytes from dba_segments where owner = USER order by bytes desc ) where rownum <= 30

 

5.查询当前用户默认表空间的使用情况

select tablespacename,sum(totalContent),sum(usecontent),sum(sparecontent),avg(sparepercent) 
from 
(
SELECT b.file_id as id,b.tablespace_name as tablespacename,b.bytes as totalContent,(b.bytes-sum(nvl(a.bytes,0))) as usecontent,sum(nvl(a.bytes,0)) as sparecontent,sum(nvl(a.bytes,0))/(b.bytes)*100  as sparepercent 
FROM dba_free_space a,dba_data_files b
WHERE a.file_id=b.file_id and b.tablespace_name = (select default_tablespace from dba_users where username = user)  
group by b.tablespace_name,b.file_name,b.file_id,b.bytes

GROUP BY tablespacename

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章