Вы находитесь на странице: 1из 16

Reclaiming wasted space in Oracle database

Author: Mounir Babari

Senior Technical Support Engineer UK

IBM B2B & Commerce - Industry Solutions

1 Reclaiming wasted space in Oracle database


Reclaiming space in Oracle can be sometimes challenging. It is a common question
from users. Some customers manage to reclaim space from tables and indexes but
find it hard for the lob segments. Starting with Oracle 10g R2, Oracle provides a
solution that allows users to reclaim LOB space (LOB data and LOB index).

In this document, I will demonstrate how to reclaim the unused space using the
segment advisor and manual commands with Oracle 11g. I will use IBM Sterling
Integrator 5 to fill a table called TRANS_DATA that contain a blob column called
DATA_OBJECT.

SQL> Desc TRANS_DATA

Name Null Type

--------------- -------- -------------

DATA_ID NOT NULL VARCHAR2(255)

DATA_OBJECT NOT NULL BLOB

PAGE_INDEX NOT NULL NUMBER(38)

DATA_TYPE NOT NULL NUMBER(38)

WF_ID NUMBER

REFERENCE_TABLE VARCHAR2(255)

First, we need to find the size of the tablespaces:

Using SQL:

select b.tablespace_name, tbs_size SizeMb, a.free_space FreeMb

from (select tablespace_name, round(sum(bytes)/1024/1024 ,2) as free_space

from dba_free_space

group by tablespace_name) a,

(select tablespace_name, sum(bytes)/1024/1024 as tbs_size

from dba_data_files

group by tablespace_name) b

where a.tablespace_name(+)=b.tablespace_name

2 Reclaiming wasted space in Oracle database


This information can also be found with the Oracle Enterprise manager under
Servers/Tablespaces.

The tablespace called SI51GM has 875MB allocated with 201MB free.

Now let us load this tablespace SI51GM using IBM Sterling Integrator:

Some of the free space was reused and the tablespace has increased to 950MB

3 Reclaiming wasted space in Oracle database


Let us upload a second large document

The size of the tablespace is 1025 MB now!!

We need to delete this data and observe the space size:

Procedure for Sterling Integrator to delete the data from the table

 update archive_info set archive_flag=1,archive_date=sysdate where wf_id=233056


 update archive_info set archive_flag=1,archive_date=sysdate where wf_id=233074
 run the purge service to delete the rows from the table TRANS_DATA

The data was removed from the tables. However, the space is not reclaimed and the
tablespace size has actually increased to 1100MB!!

4 Reclaiming wasted space in Oracle database


The Database usage indicator is in the red zone!!

We are only using about 50MB for this blob column, we use the dbms_lob to find the size of
the binary objects:

select sum(dbms_lob.getlength(data_object))/1024/1024, count(*) from


trans_data

We have about 500 MB used between TRANS_DATA table and its LOB segment:

select segment_name,segment_type,tablespace_name,round(bytes/1024/1024) MB
from user_segments where tablespace_name ='SI51GM' order by bytes desc

5 Reclaiming wasted space in Oracle database


Reclaiming the space with the Segment Advisor

The Segment Advisor identifies segments that have space available for reclamation.
It performs its analysis by examining usage and growth statistics in the Automatic
Workload Repository (AWR), and by sampling the data in the segment. It is
configured to run during maintenance windows as an automated maintenance task,
and you can also run it on demand (manually). The Segment Advisor automated
maintenance task is known as the Automatic Segment Advisor.

The Segment Advisor creates several types of results: recommendations, findings,


actions, and objects. You can view results in the following ways:

 With Enterprise Manager


 By querying the DBA_ADVISOR_* views
 By calling the DBMS_SPACE.ASA_RECOMMENDATIONS procedure

For further reading see also:

 Oracle Database Reference for details on the DBA_ADVISOR_* views.


 Oracle Database PL/SQL Packages and Types Reference for details on the
DBMS_SPACE.ASA_RECOMMENDATIONS function.

Segment advisor on the SI51GM tablespace

6 Reclaiming wasted space in Oracle database


The dbms_advisor is used in the background:

DECLARE

taskname varchar2(100);
taskdesc varchar2(128);
task_id number;
object_id number;
timeLimit varchar2(25);
numDaysToRetain varchar2(25);
objectName varchar2(100);
objectType varchar2(100);

BEGIN
taskname := 'SEGMENTADV_2503141';
taskdesc :='Get shrink advice based on object growth trend';
numDaysToRetain :='30';
dbms_advisor.create_task('Segment Advisor',?,taskname,taskdesc,NULL);
dbms_advisor.create_object(taskname, 'TABLESPACE', 'SI51GM', ' ', ' ',
NULL, object_id);
dbms_advisor.set_task_parameter(taskname, 'RECOMMEND_ALL', 'TRUE');
dbms_advisor.set_task_parameter(taskname, 'DAYS_TO_EXPIRE',
numDaysToRetain);
END;

7 Reclaiming wasted space in Oracle database


8 Reclaiming wasted space in Oracle database
The Segment Advisor identified the following segments that have space available for
reclamation.

The segment advisor found 247MB to reclaim from the TRANS_DATA table. Below is
the SQL query to shrink the table:

PL/SQL
begin
EXECUTE IMMEDIATE 'alter table "SI51GM"."TRANS_DATA" shrink space';
end;

9 Reclaiming wasted space in Oracle database


We can see that some space was freed from the table but not from the LOB
segment:

10 Reclaiming wasted space in Oracle database


Running the Segment Advisor manually:

We need to run the segment advisor manually to analyze the LOB segment for the
TRANS_DATA table. The following procedures enables you to include the schema
object's dependent objects in the Segment Advisor run. For example, if you select a
table and select the Run Segment Advisor command, Enterprise Manager displays
the table's dependent objects, such as partitions, index segments, LOB segments,
and so on. You can then select dependent objects to include in the run.

To run the Segment Advisor manually with the Segment Advisor Wizard:

1. From the database Home page, under Related Links, click Advisor Central.

The Advisor Central page appears

Under Advisors, click Segment Advisor.

The first page of the Segment Advisor wizard appears.

2. Follow the wizard steps to schedule the Segment Advisor job, and then click
Submit on the final wizard page.

The Advisor Central page reappears, with the new Segment Advisor job at the
top of the list under the Results heading. The job status is SCHEDULED. (If
you don't see your job, use the search fields above the list to display it.)

3. Check the status of the job. If it is not COMPLETED, click the Refresh
button at the top of the page repeatedly. (Do not use your browser's Refresh
icon.)

When the job status changes to COMPLETED, select the job by clicking in the
Select column, and then click View Result.

11 Reclaiming wasted space in Oracle database


12 Reclaiming wasted space in Oracle database
Some space has been reclaimed from different objects but not from the lob
segment:

13 Reclaiming wasted space in Oracle database


After running the shrink space command on the blob column we get more space
reclaimed:

The syntax used to reclaim LOB space is:

SQL> ALTER TABLE <table_name> MODIFY LOB (<lob_column>) (SHRINK SPACE);

The alter table <table_name> shrink space statement was


introduced in Oracle 10g R1. The ability to extend the SHRINK SPACE
command to LOBs was introduced in Oracle 10g R2

SQL> alter table trans_data modify lob (data_object) (shrink space);

After running the previous command, we notice that the


SYS_LOB0000160841C00002$$ is not even in the top list:

SQL Query to list all the lob segments for a given tablespace:
select segment_name,segment_type,tablespace_name, round(bytes/1024/1024) MB
from user_segments where tablespace_name='SI51GM' and
segment_type='LOBSEGMENT' order by bytes desc

14 Reclaiming wasted space in Oracle database


The space now is reclaimed and the Sterling Integrator database screen usage
indicator is back to the green zone:

15 Reclaiming wasted space in Oracle database


DBA tips:

 The LOB segment must reside in an Automatic Segment Space Management


ASSM tablespace.

 Row movement on a table is only required if you are moving rows from the
table itself. It is not required if all you are doing is shrinking its LOB
segment(s).

 Shrinking a LOB segment does generate redo. For example, shrinking a 15GB
LOB will generate approximately 15GB of redo! Make sure you have an
adequate amount of disk space for any archived redo log files before
manually shrinking a LOB segment.

 The alter table <table_name> shrink space statement has an optional


CASCADE clause that shrinks all dependent objects (including LOBS) along
with the table data itself.

 With Oracle 10g R1, when shrinking a table, the CASCADE option DOES
NOT shrink LOB segments for that table.

 With Oracle 10g R2 and higher, when shrinking a table, the CASCADE option
DOES shrink all LOB segments (and indexes) for that table

16 Reclaiming wasted space in Oracle database