First analyze big tablespace to see how much space can be reclaimed before performing the shrink. You need to provide tablespace name and shrink and shrink mode to perform this task.
- TS_MODE_ANALYZE – This mode analyzes the tablespace
- TS_MODE_SHRINK – This mode is default, and it shrinks tablespace to reclaim space
- TS_MODE_SHRINK_FORCE - This mode moves objects online and in case of failure it will attempt to move offline.
You should set “serveroutput” g on to show the results on the terminal
SQL> set serveroutput on;
Analyze how much space can be reclaimed using TS_MODE_ANALYZE mode
SQL> execute DBMS_SPACE.SHRINK_TABLESPACE (‘USER_TBS1’, SHRINK_MODE => DBMS_SPACE.TS_MODE_ANALYZE);
PL/SQL procedure successfully completed.
Here is the simplified version of the output
ANALYZE RESULTS
Total Movable Objects: 2
Total Movable Size(GB): 1.32
Original Datafile Size(GB): 5
Suggested Target Size(GB): 2.59
Process Time: +00 00:00:00.701704
Now you can perform the actual shrink operation to reclaim the space, note that DBMS_SPACE.TS_MODE_SHRINK is used by default if you don’t mention explicitly. If you don’t mention any TARGET_SIZE by default it will use DBMS_SPACE.TS_TARGET_MAX_SHRINK
SQL> execute DBMS_SPACE.SHRINK_TABLESPACE (‘USER_TBS1’);
PL/SQL procedure successfully completed.
Or
SQL> execute DBMS_SPACE.SHRINK_TABLESPACE (‘USER_TBS1’, SHRINK_MODE => DBMS_SPACE.TS_MODE_SHRINK);
PL/SQL procedure successfully completed.
Here is the simplified version of the output
SHRINK RESULTS
Total Moved Objects: 2
Total Moved Size(GB): 1.32
Original Datafile Size(GB): 5
New Datafile Size(GB): 1.44
Process Time: +00 00:00:21.501692
PL/SQL procedure successfully completed.
Note that you may not be able to reduce exact size that you got from analyze.
You can run below query to get the size of the tablespace.
SQL> SELECT tablespace_name, sum(bytes)/1024/1024/1024 as ”Tablespace Size (GB)”
FROM dba_data_files
GROUP BY tablespace_name;
You can run below query to check the segment size and you can add where condition for specific segment name or type
FROM dba_data_files
GROUP BY tablespace_name;
You can run below query to check the segment size and you can add where condition for specific segment name or type
SQL> SELECT segment_name, segment_type, bytes/1024 size_kb
FROM user_segments;
DBMS_SPACE package has below subprograms and used for various operations: -
FROM user_segments;
DBMS_SPACE package has below subprograms and used for various operations: -
ASA_RECOMMENDATIONS Function - This function returns recommendations
SPACE_USAGE Procedure - This procedure shows the usage of data blocks under high water mark and auto segment space management
UNUSED_SPACE Procedure - This procedure shows unused space in an object
SHRINK_TABLESPACE Procedure - This procedure used to resize or analyze big file tablespace
OBJECT_DEPENDENT_SEGMENTS Function - This function returns the list of segments associated with an object
OBJECT_GROWTH_TREND Function - This function provides space usage of object at a specific point-in-time
FREE_BLOCKS Procedure - This procedure provides information about free blocks
ISDATAFILEDROPPABLE_NAME Procedure - This procedure checks datafile is droppable
CREATE_INDEX_COST Procedure - This procedure determines cost of creating an index
CREATE_TABLE_COST Procedure - This procedure determines the size of the table
The Shrink Tablespace simplifies the procedure to reclaim the free space from datafiles by organizing objects.
Thanks & Regards,