Oracle磁盤空間使用統計

對於大型數據庫,Oracle佔用的磁盤空間很是大,掌握數據庫中那些用戶、表佔用了多殺磁盤空間,以及增加狀況,能夠方便往後對磁盤系統進行維護和擴充。數據庫

對Oracle磁盤空間使用狀況,能夠分爲按照表空間、用戶或者表來進行統計。ui

(一)、表空間spa

計算表空間的剩餘大小orm

select A.TABLESPACE_NAME,A.BYTES/(1024*1024*1024) "SPACE(G)", C.BYTES/(1024*1024) "FREE SPACE(M)",(C.BYTES*100)/A.BYTES "% FREE" FROM SYS.SM$TS_AVAIL A,SYS.SM$TS_FREE C WHERE A.TABLESPACE_NAME=C.TABLESPACE_NAME;

或者索引

select tablespace_name, sum(bytes)/(1024*1024*1024) "SPACE(G)" from dba_free_space group by tablespace_name;

(二)、用戶資源

計算每一個用戶佔用的磁盤空間it

select owner,sum(bytes)/1024/1024/1024 "Space(G)" from dba_segments group by owner order by 2;

計算某個用戶佔用的磁盤空間table

select owner,sum(bytes)/1024/1024/1024 "Space(G)" from dba_segments where owner='LIAOJL' group by owner;

(三)、表form

Oracle都是以段爲存儲的,segment_name包含了表、索引、回滾段等,因此在dba_extents,dba_segments均可以找到佔用空間大小的信息。class

select sum(bytes)/1024/1024 "Space(M)" from dba_extents where owner='LIAOJL' and segment_name='STUDENTS';

dba_segments也能夠計算表的大小:

select segment_name,bytes/1024/1024 "Space(MB)" from dba_segments where SEGMENT_TYPE='TABLE' and segment_name=upper('你要查找的表的名字');

當時上面的寫法不徹底正確,當表是分區表,dba_segments有多條信息,可改爲:

select segment_name,sum(bytes)/1024/1024 "Space(MB)" from dba_segments where segment_name=upper('你要查找的表的名字');

上述方法對於一個很大的數據庫,SQL語句執行起來會很慢,並且消耗數據庫資源。Oracle支持對錶進行分析,執行分析表操做後能夠在dba_tables等系統表中查詢表大小、行數等信息,不過這些信息不是實時更新的,能夠在數據庫空閒時,經過計劃任務來更新。

分析SQL方法:

analyze table tab_name compute statistics;

表太大的話能夠執行:

analyze table tab_name estimate statistics;
/*SELECT tablespace_name, sum ( blocks ) as free_blk ,
  trunc ( sum ( bytes ) /(1024*1024) ) as free_m,
  max ( bytes ) / (1024) as big_chunk_k, count (*) as num_chunks
  FROM dba_free_space GROUP BY tablespace_name;*/
/*
* SqlName: obj_seg
* 2006-11-17 By HuiYi
* 查詢對象大小
*/
select owner,sum(bytes)/1024/1024/1024 "Space(G)" from dba_segments group by owner order by 2 desc
col segment_name format a30 col segment_type format a15 col tablespace_name format a10 col bytes heading "Size(M)" with t as( select segment_name,owner,owner||'.'||segment_name,        segment_type,        tablespace_name,        bytes / 1024 / 1024 bytes   from dba_segments where owner = upper('&user') order by bytes desc ) select * from t where rownum<10;
相關文章
相關標籤/搜索