Shrinking Temporary Tablespaces
Shrinking Temporary Tablespaces
When a large job that uses a temporary tablespace finishes executing, the database doesn’t
immediately release the space used by the job in the temporary tablespace, even after the
job completes. You can get the free space back faster sometimes by dropping the
temporary tablespace and creating a smaller one instead, but then it may not be easy to do
this on a live database because users may require the temporary tablespace for various
operations. Oracle Database 11g lets you shrink a temporary tablespace online, thus
enabling you to control the space allocated to temporary operations in the database. You
can shrink both temporary tablespaces and individual tempfiles.
Use the shrink space clause within an alter tablespace command to shrink a
temporary tablespace, as shown here:
The shrink space command in the first example shrinks all tempfiles to a database–
determined minimum size, 1 MB. The database takes into account the
temporary tablespace storage requirements when determining the minimum size of the
tempfiles. You can override this default behavior by specifying a minimum size for the
temporary tablespace after a shrink operation, as shown here:
The keep clause lets you specify the minimum value for the
temporary tablespace named TEMP. The following examples illustrate how Oracle
approaches a temporary tablespace shrinking operation. In this example, the
temporary tablespace TEMP has two tempfiles, each sized at 1 GB, thus making the total
size of the temporary tablespace 2 GB. You issue the following alter tablespace statement
to shrink the temporarytablespace to 1 GB.
Because the combined size of the two tempfiles in the TEMP tablespace is 2 GB, you’d
assume that Oracle would shrink both tempfiles to about 500 MB each, to get a total of 1
GB, when you issue the alter tablespace statement shown here. However, this isn’t what
happens, as you can see by issuing the following query:
Oracle does shrink the TEMP tablespace from 2 GB to 1 GB, but not by shrinking both
tempfiles by an equal amount. It shrinks the file temp01.dbf by less than 1 MB and the file
temp02.dbf by over 999 MB. You can specify a minimum space that the database must
retain in a specific tempfile, by specifying the keep clause in
the alter tablespace … shrink statement, as follows:
This statement will shrink just the datafile temp02 and leave the other tempfiles in
the tablespace alone.
You can query the new DBA_TEMP_FREE_SPACE view to get information about
temporary tablespace usage, as shown in this example:
The DBA_TEMP_FREE_SPACE view shows the total free space available, including the
space currently allocated to a temporary tablespaceand available for reuse as well as space
that’s currently unallocated.