Bigfile Tablespace Shrink in Oracle Database 23ai/26ai
From Oracle database 23ai/26ai onward we can use the DBMS_SPACE package to shrink a bigfile tablespace to reclaim unused space.
oracle 23configurationintermediate
by OracleDba
22 views
From Oracle database 23ai/26ai onward we can use the DBMS_SPACE package to shrink a bigfile tablespace to reclaim unused space.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
conn sys/SysPassword1@//localhost:1521/freepdb1 as sysdba
-- Create a tablespace and user for the test.
drop user if exists reclaim_user cascade;
drop tablespace if exists reclaim_ts including contents and datafiles;
create tablespace reclaim_ts datafile size 10m autoextend on next 1m;
create user reclaim_user identified by reclaim_user default tablespace reclaim_ts quota unlimited on reclaim_ts;
grant create session, create table to reclaim_user;
grant select_catalog_role to reclaim_user;
-- Create and populate two tables in the test schema.
conn reclaim_user/reclaim_user@//localhost:1521/freepdb1
create table t1 (
id number,
col1 varchar2(4000),
col2 varchar2(4000),
constraint t1_pk primary key (id)
);
create table t2 (
id number,
col1 varchar2(4000),
col2 varchar2(4000),
constraint t2_pk primary key (id)
);
insert /*+append*/ into t1
select rownum, rpad('x', 4000, 'x'), rpad('x', 4000, 'x')
from dual
connect by level <= 100000;
commit;
insert /*+append*/ into t2
select rownum, rpad('x', 4000, 'x'), rpad('x', 4000, 'x')
from dual
connect by level <= 100000;
commit;
exec dbms_stats.gather_table_stats(null, 't1');
exec dbms_stats.gather_table_stats(null, 't2');
column file_name format a30
select substr(file_name, -28) as file_name, blocks, bytes/1024/1024 as size_mb
from dba_data_files
where tablespace_name = 'RECLAIM_TS';
FILE_NAME BLOCKS SIZE_MB
------------------------------ ---------- ----------
o1_mf_reclaim__mtn1tkxs_.dbf 427520 3340
SQL>
column table_name format a10
select table_name, blocks, (blocks*8)/1024 as size_mb
from user_tables
where table_name in ('T1', 'T2')
order by 1;
TABLE_NAME BLOCKS SIZE_MB
---------- ---------- ----------
T1 200696 1567.9375
T2 200694 1567.92188
SQL>
truncate table t1;
exec dbms_stats.gather_table_stats(null, 't1');1234567891011121314
conn sys/SysPassword1@//localhost:1521/freepdb1 as sysdba
set serveroutput on
execute dbms_space.shrink_tablespace('RECLAIM_TS', shrink_mode => dbms_space.ts_mode_analyze);
-------------------ANALYZE RESULT-------------------
Total Movable Objects: 2
Total Movable Size(GB): 1.56
Original Datafile Size(GB): 3.26
Suggested Target Size(GB): 3.19
Process Time: +00 00:00:00.950648
PL/SQL procedure successfully completed.
SQL>123456789101112131415161718192021222324252627
set serveroutput on
execute dbms_space.shrink_tablespace('RECLAIM_TS');
-------------------SHRINK RESULT-------------------
Total Moved Objects: 2
Total Moved Size(GB): 1.56
Original Datafile Size(GB): 3.26
New Datafile Size(GB): 1.63
Process Time: +00 00:00:17.782739
PL/SQL procedure successfully completed.
SQL>
column file_name format a30
select substr(file_name, -28) as file_name, blocks, bytes/1024/1024 as size_mb
from dba_data_files
where tablespace_name = 'RECLAIM_TS';
FILE_NAME BLOCKS SIZE_MB
------------------------------ ---------- ----------
o1_mf_reclaim__mtn1tkxs_.dbf 214016 1672
SQL>
set serveroutput on
execute dbms_space.shrink_tablespace('RECLAIM_TS', shrink_mode => dbms_space.ts_mode_shrink, target_size => dbms_space.ts_target_max_shrink);Please to add comments
No comments yet. Be the first to comment!