Oracle datafiles shrinking
Submitted by madvip on Fri, 02/29/2008 - 13:47.
As most of you already know, deleting rows from a table doesn't deallocate space. The only way to shrink the datafiles is by using the ALTER DATAFILE...RESIZE statement, but we must know by how much we can shrink it! Tom Kyte provided this nifty script which will generate the ALTER DATABASE commands to shrink datafiles to their smallest possible size:
column value new_val blksize
select value
from v$parameter
where name = 'db_block_size'
/
select 'alter database datafile ''' ||
file_name || ''' resize ' ||
ceil( (nvl(hwm,1)*&&blksize)/1024/1024 )
|| 'm;' cmd
from dba_data_files a,
( select file_id,
max(block_id+blocks-1) hwm
from dba_extents
group by file_id ) b
where a.file_id = b.file_id(+)
and
ceil(blocks*&&blksize/1024/1024)-
ceil((nvl(hwm,1)*
&&blksize)/1024/1024 ) > 0
/

