Decreasing tablespace size
I have an Oracle8i Database Server, which has a tablespace with 16 data files amounting to 32 GB of disk space usage. I have deleted a lot records that are using these tablespaces, but its apparent that the tablespace size will not decrease. How do I go ahead with freeing up space? I cannot create a new table and move data to it.
One of the principles of any database management system is to do as little work as possible. With that in mind, when you delete rows of data from a table, Oracle does not deallocate this once-used space. Instead, it leaves this space allocated for future insertions into the table. So how do you reclaim this space? First, you'll want to copy the data in that table to a temporary table. This can be done with a statement as simple as follows:
CREATE TABLE my_temp_table AS SELECT * FROM orig_table;
Then, disable any foreign key constraints to and from this original table. Also disable any triggers on the table. Next, you can remove all remaining data and reclaim all space, except for the INITIAL extent, by issuing the following command:
TRUNCATE TABLE orig_table;
Then, move the data back into the original table:
INSERT INTO orig_table SELECT * FROM my_temp_table;
Finally, enable any constraints or triggers that you disabled and drop
the temporary table.
This was first published in December 2003