Friday, April 28, 2006

How to adjust the high watermark in ORACLE 10g - ALTER TABLE SHRINK

Hanging around in my hotel in Chicago because it is raining, I have been cruising around in the OTN forums which inspired me to write something about the High Watermark and the Oracle 10gR1 New Feature SEGMENT SHRINKING.

The High Watermark is the maximum fill-grade a table has ever reached.Above the high watermark are only empty blocks.These blocks can be formatted or unformatted.

First let's have a look at the question when space is allocated- when you create a table at least one extent (contiguous blocks) is allocated to the table- if you have specified MINEXTENTS the number of MINEXTENTS extentswill be allocated immedaitely to the table- if you have not specified MINEXTENTS then exactely one extentwill be allocated (we will look at extent sizes later in another post).

Immediately after creation of the segment (table) the high watermark will be at the first block of the first extent as long as there are no inserts made.

When you insert rows into the table the high watermark will be bumped up step by step.This is done by the server process which makes the inserts.

Now let us take a look at when space is released again from a segment like a table or index:

Let's asume that we have filled a table with 100'0000 rows.And let's asume that we deleted 50'000 rows afterwards.In this case the high watermark will have reached the level of 100'000 and will have stayed there. Which means that we have empty blocks below the high watermark now.Oracle has a good reason this: it might occur that you delete rows and immediately this you insert rows into the same table. In this case it is good that the space was not released with the deletes, because it had to be get reallocate again for the following inserts, which would mean permanent changes to the data dictionary(=> dba_free_space, dba_extents, dba_segements ...) .Furthermore the physical addresses of the deleted row get recycled by new rows.

These empty blocks below the high watermark can get annoying in a number of situations because they are not used by DIRECT LOADs and DIRECT PATH LOADs:

All the above actions case that the SGA is not used for the inserts but the PGA:there wil be temporary segements filled and dumped into newly formatted blocks above the high watermark.

So we might want to get high watermark down before we load data into the table in order to use the free empty blocks for the loading.

So how can we release unused space from a table?There are a number of possible options which are already available before Oracle 10g:- What we always could do is export and import the segment. After an import the table will have only one extent. The rows will have new physical addresses and the high watermark will be adjusted.- Another option would be to TRUNCATE the table. With this we would loose all rows which are in the table. So we cannot use this if we want to keep existing records.

With Oracle 9i another possibilty was implemented:ALTER TABLE emp MOVE TABLESPACE users;This statement will also cause that- the rows will have new physical addresses and- the high watermark will be adjusted.But for this:- we need a full (exclusive) table lock- the indexes will be left with the status unusable (because they contain the old rowids) and must be rebuilt.

Starting with ORACLE 10gR1 we can use a new feature for adjusting the high watermark,it is called segment shrinking and is only possible for segments which use ASSM, in other words, which are located in tablespaces which use Automatic Segement Space Management.In such a tablespace a table does not really have a High watermark!It uses two watermarks instead:- the High High Watermark referred to as HHWM, above which alle blocks ar unformatted.- the Low High Watermark referred to as LHWM below which all blocks are formatted.We now can have unformatted blocks in the middle of a segment!

ASSM was introduced in Oracle 9iR2 and it was made the default for tablespaces in Oracle 10gR2.With the table shrinking feature we can get Oracleto move rows which are located in the middle or at the end of a segmentfurther more down to the beginning of the segment and bythis make the segment more compact. For this we must first allow ORACLE to change the ROWIDs of these rows by issuing ALTER TABLE emp ENABLE ROW MOVEMENT;ROWIDs are normally assigned to a row for the life time of the row at insert time.

After we have given Oracle the permission to change the ROWIDs we can now issue a shrink statement.ALTER TABLE emp SHRINK SPACE;This statement will procede in two steps:- The first step makes the segment compactby moving rows further down to free blocks at the beginning of the segment.- The second step adjusts the high watermark. For this Oracle needs an exclusive table lock,but for a very short moment only.

Table shrinking...- will adjust the high watermark- can be done online- will cause only rowlocks during the operation and just a very short full table lock at the end of the operation- indexes will be maintained and remain usable- can be made in one go- can be made in two steps(this can be usefull if you cannot get a full table lock during certain hours:you only make the first step and adjust the high watermark laterwhen it is more conveniant:

1.)It is only possible in tablespaces with ASSM.2.) You cannot shrink:- UNDO segments- temporary segments- clustered tables- tables with a colmn of datatype LONG- LOB indexes- IOT mapping tables and IOT overflow segments- tables with MVIEWS with ON COMMIT- tables with MVIEWS which are based on ROWIDs

The Oracle 10g Oracle comes with a Segment Advisor utility.The Enterprise Manager, Database Control, even has a wizzard which can search for shrink candidates.

This advisor is run automatically by an autotask job on a regular basis in the default maintainance window.

You can use the built in package DBMS_SPACE to run the advisor manually as well.I will blog about this later on some time.

2 Comments:

1. Is there any way to do "ALTER INDEX IND1 SHRINK SPACE" in parallel ?2. As when I try to run this command to reclaim around 1.6 GB from a 5 GB index segment, I am not sure how much time it will take, as just to re-claim 400 MB space it took 30 mins in a 16 CPU env.3. Is it safer to do while the db is up & running in a collabsuite env ?