查詢資料檔案大小和實際大小,並收縮資料檔案(原創)

coolhe發表於2010-11-16
查詢資料檔案大小和實際大小,並收縮資料檔案(原創)
在實際的生產過程中,實際的資料檔案大小或者表空間大小和實有資料大小是不相符的,所以有時有必要進行表空間,或者資料檔案進行收縮。

以下是我寫的一個sql以供大家方便執行。

--查詢並收縮資料檔案
select m1.file_id "檔案#"   ,
       m1.name "檔名",
       m1.size_mb_fact "實際檔案大小(MB)", 
       m1.size_mb_data "資料佔有大小(MB)", 
       round(m1.size_mb_fact*100/ m1.size_mb_data, 2) "資料和檔案的比率",
       case when round(m1.size_mb_fact*100/ m1.size_mb_data, 2)>110 and m1.size_mb_fact>4 then  --大於110則進行收縮資料檔案
            'alter database datafile '''||m1.name||''' resize '||to_char(round(m1.size_mb_data*1.2,0))||'M;'
            else null end "收縮資料檔案命令"
  from (select file_id, m.name, m.size_mb_fact, max(block_id)*8192/1024/1024 size_mb_data
          from dba_extents a, 
               (select b.file#, b.name, b.BYTES/1024/1024 size_mb_fact from v$datafile b) m
         where a.file_id = m.file#
         group by a.file_id, m.name, m.size_mb_fact
       ) m1
   order by "資料和檔案的比率" desc;

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/3090/viewspace-678200/,如需轉載,請註明出處,否則將追究法律責任。

相關文章