Professional Documents
Culture Documents
ALTER TABLE ... SHRINK SPACE Command : Online Segment Shrink for
Tables, LOBs and IOTs
The ALTER TABLE ... SHRINK SPACE command was introduced in Oracle 10g to perform online segment shrinks for
tables, LOBs and IOT overflow segments.
-- Recover space, but don't amend the high water mark (HWM).
ALTER TABLE scott.emp SHRINK SPACE COMPACT;
The COMPACT option allows the shrink operation to be broken into two stages. First the rows are moved using the COMPACT option but the high water mark (HWM) is not
adjusted so no parsed SQL statements are invalidated. The HWM can be adjusted at a later date by reissuing the statement without the COMPACT option. At this point any
dependent SQL statements will need to be re-parsed.
SELECT *
FROM (SELECT owner,
segment_name,
segment_type,
tablespace_name,
ROUND(bytes/1024/1024,2) size_mb
FROM dba_segments
ORDER BY 5 DESC)
WHERE ROWNUM <= 20;
You may see many of the larger segments being LOB segments. You can get more information about LOB segments specifically using the following top-n query.
SET LINESIZE 200
COLUMN owner FORMAT A30
COLUMN table_name FORMAT A30
COLUMN column_name FORMAT A30
COLUMN segment_name FORMAT A30
COLUMN tablespace_name FORMAT A30
COLUMN size_mb FORMAT 99999999.00
SELECT *
FROM (SELECT l.owner,
l.table_name,
l.column_name,
l.segment_name,
l.tablespace_name,
ROUND(s.bytes/1024/1024,2) size_mb
FROM dba_lobs l
JOIN dba_segments s ON s.owner = l.owner AND s.segment_name = l.segment_name
ORDER BY 6 DESC)
WHERE ROWNUM <= 20;
large_segments.sql
large_lob_segments.sql
Row Movement
The ALTER TABLE ... SHRINK SPACE command moves rows between existing blocks to compact the data, so before you attempt to shrink a table segment you need to
enable row movement. You can check if row movement is already enabled by querying the ROW_MOVEMENT column of the [DBA|ALL|USER]_TABLES views.
SELECT row_movement
FROM user_tables
WHERE table_name = 'EMP';
ROW_MOVE
--------
DISABLED
SQL>
SELECT row_movement
FROM user_tables
WHERE table_name = 'EMP';
ROW_MOVE
--------
ENABLED
SQL>
SecureFile LOBs
When using basicfile LOBs the shrink commands work as expected. To demonstrate this we need to create the following table containing a basicfile LOB column.
Table altered.
Table altered.
SQL>
Now the first command fails, but adding the CASCADE option appears to make it work.
Table altered.
SQL>
Unfortunately, the second command doesn't work and the securefile LOB segment is not shrunk.
Instead, to shrink a securefile LOB segment you need to move it. In the following example the move is to the same tablespace.
ALTER TABLE
Online Segment Shrink for Tables : Free Unused Space
Reclaiming Unused Space in Datafiles
Excelentia Comprar
Contact Us
Home | Articles | Scripts | Blog | Certification | Videos | Misc | About