Wednesday, August 12, 2015

Oracle : Script that shows tablespace free space

select /*+ CURSOR_SHARING_EXACT */
  substrb(dtb.tablespace_name,1,16) TS_NAME,
  substrb(dtb.contents,1,3) TYP,
  substrb(dtb.status,1,2) ST,
  substrb(decode(dtb.logging,'LOGGING','Y','N'),1,1) "L",
  substrb(dtb.extent_management,1,1) "D",
  substrb(decode(floor(dtb.initial_extent/1073741824),0,
    decode(floor(dtb.initial_extent/1048576),0,
      to_char(dtb.initial_extent/1024,'9999')||'K',
      to_char(dtb.initial_extent/1048576,'9999')||'M'),
    to_char(dtb.initial_extent/1073741824,'99.9')||'G'),2,6) "INIT",
  substrb(decode(dtb.allocation_type,'UNIFORM',' UEM','SYSTEM',' SYS',
    decode(floor(dtb.next_extent/1073741824),0,
      decode(floor(dtb.next_extent/1048576),0,
        to_char(dtb.next_extent/1024,'9999')||'K',
        to_char(dtb.next_extent/1048576,'9999')||'M'),
      to_char(dtb.next_extent/1073741824,'99.9')||'G')),2,6) "NEXT",
  substrb(to_char(my_dba_data_files.file_count,'999'),2,4) FC,
  substrb(to_char(my_dba_data_files.auto_extend,'999'),2,4) AX,
  substrb(decode(floor(my_dba_data_files.total_size/1073741824),0,
    decode(floor(my_dba_data_files.total_size/1048576),0,
      to_char(my_dba_data_files.total_size/1024,'9999')||'K',
      to_char(my_dba_data_files.total_size/1048576,'9999')||'M'),
    to_char(my_dba_data_files.total_size/1073741824,'99.9')||'G'),2,6) "SIZE",
  substrb(decode(floor(my_free_space.total_free_space/1073741824),0,
    decode(floor(my_free_space.total_free_space/1048576),0,
      to_char(my_free_space.total_free_space/1024,'9999')||'K',
      to_char(my_free_space.total_free_space/1048576,'9999')||'M'),
    to_char(my_free_space.total_free_space/1073741824,'99.9')||'G'),2,6) "FREE",
  substrb(decode(floor(my_dba_data_files.max_size/1073741824),0,
    decode(floor(my_dba_data_files.max_size/1048576),0,
      to_char(my_dba_data_files.max_size/1024,'99999')||'K',
      to_char(my_dba_data_files.max_size/1048576,'99999')||'M'),
    to_char(my_dba_data_files.max_size/1073741824,'999.9')||'G'),2,7) "MAX",
  substrb(decode(floor(my_free_space.largest_free_segment/1073741824),0,
    decode(floor(my_free_space.largest_free_segment/1048576),0,
      to_char(my_free_space.largest_free_segment/1024,'9999')||'K',
      to_char(my_free_space.largest_free_segment/1048576,'9999')||'M'),
    to_char(my_free_space.largest_free_segment/1073741824,'99.9')||'G'),2,6) LFS,
  lpad(decode(my_dba_data_files.auto_extend,0,' ','(')||
    ltrim(to_char(least(nvl(((my_free_space.total_free_space+(my_dba_data_files.max_size-my_dba_data_files.total_size))/my_dba_data_files.max_size)*100,0),99.9),'99.9'))||
'%'||decode(my_dba_data_files.auto_extend,0,' ',')'),7) PC_FREE
from dba_tablespaces dtb, (
  select
    ddf.tablespace_name,
    count(ddf.file_id) file_count,
    sum(ddf.bytes) total_size,
    sum(greatest(ddf.bytes,ddf.maxbytes)) old_max_size,
    sum(decode(ddf.autoextensible,'YES',greatest(ddf.bytes,ddf.maxbytes),ddf.bytes)) max_size,
    sum(decode(ddf.autoextensible,'YES',1,0)) auto_extend
  from dba_data_files ddf
  group by ddf.tablespace_name ) my_dba_data_files, (
    select
    dfs.tablespace_name tablespace_name,
    max(dfs.bytes) largest_free_segment,
    sum(dfs.bytes) total_free_space
  from dba_free_space dfs
  group by tablespace_name
  union all
  select
    dtb.tablespace_name,0,0
  from dba_tablespaces dtb
  where not exists ( select 1 from dba_free_space dfs where dfs.tablespace_name = dtb.tablespace_name )) my_free_space
where dtb.tablespace_name = my_dba_data_files.tablespace_name(+)
and dtb.tablespace_name = my_free_space.tablespace_name(+)
order by
  ((my_free_space.total_free_space+(my_dba_data_files.max_size-my_dba_data_files.total_size))/my_dba_data_files.max_size) desc,
  typ,
  ts_name
/

No comments:

Post a Comment