Oracle – Table Size Unaffected After Deleting Rows

oracle

When I ran the below query

select bytes/1024/1024,segment_name,owner from dba_segments where tablespace_name='DATA_L1' and segment_name='Step_Status';

I got the below result for a segment which is a table named 'Step_Status'

BYTES/1024/1024         SEGMENT_NAME                OWNER
-----------------------------------------------------------------
 3192                    STEP_STATUS                FLSTD04

It had about 18 million entries

select count(*) from flstd04.step_status;

  COUNT(*)
----------
  18178090

After deleting nearly 4 million enteries

select count(*) from flstd04.step_status;

  COUNT(*)
----------
  14114186

When I again checked the size of the table using dba_segments
I got the same result


select bytes/1024/1024,segment_name,owner from dba_segments where tablespace_name='DATA_L1' and segment_name='Step_Status';

  BYTES/1024/1024         SEGMENT_NAME                OWNER
    -----------------------------------------------------------------
   3192                    STEP_STATUS                FLSTD04

Why is it that even after deleting 4 million rows the size of the table is same?

Best Answer

Deleting data isn't expected to change the size of the table segment. It will create free space in blocks that are part of the table segment and it will probably create at least a few empty blocks. That free space will be made available for subsequent insert operations on the table (assuming you're doing conventional-path inserts) as well as update operations that cause the size of rows to increase.

If you want to actually shrink the size of the table segment (generally not a good idea if you're just going to insert more data into this table in the future), depending on the Oracle version, the type of tablespace, and whether you've enabled row movement you can probably shrink the segment

ALTER TABLE step_status
  SHRINK SPACE CASCADE;

Be aware, however, that this may be a very costly operation that may involve physically moving most or all of the rows in the table.

There is a chapter in the Database Administrator's Guide on reclaiming wasted space that discusses this and related issues in much more depth. It also walks through using the Enterprise Manager GUI and the various advisors to reclaim space either manually or automatically.