How To Use Temporary Tablespaces

Temporary tablespaces are used to manage space for database sort operations and for storing global temporary tables.

Each database should have one temporary tablespace that is created when the database is created. You can create, drop and manage tablespaces with create temporary tablespace, drop temporary tablespace and alter temporary tablespace commands.

Allocate temporary tablespace to each user in the daabase, so we can avoid from sort space in the System tablespace.

SQL> CREATE USER scott DEFAULT TABLESPACE data TEMPORARY TABLESPACE temp;
SQL> ALTER USER scott TEMPORARY TABLESPACE temp;


You can remove a TEMPFILE from a database.

SQL> ALTER DATABASE TEMPFILE '/db2/oradata/dev/data/temp02.dbf' DROP INCLUDING DATAFILES;

If you remove all tempfiles from a temporary tablespace, you may encounter error:
ORA-25153: Temporary Tablespace is Empty. So add a TEMPFILE to a temporary tablespace:

SQL>ALTER TABLESPACE temp ADD TEMPFILE '/db2/oradata/dev/data/temp002.dbf' SIZE 200M;

SQL> ALTER TABLESPACE temp OFFLINE

SQL> DROP TABLESPACE temp;

HOW TO create Temporary Tablespaces?

SQL> CREATE TEMPORARY TABLESPACE temp
TEMPFILE '/db2/oradata/dev/data/temp01.dbf' SIZE 3000M
EXTENT MANAGEMENT LOCAL UNIFORM SIZE 16M;

Add Temp Datafiles

SQL> ALTER TABLESPACE temp
ADD TEMPFILE '/db2/oradata/dev/data/temp02.dbf' SIZE 2000M REUSE;

How to Set Default Temporary Tablespaces

SQL> ALTER DATABASE DEFAULT TEMPORARY TABLESPACE temp;

- The Default Temporary Tablespace must be of type TEMPORARY
- The DEFAULT TEMPORARY TABLESPACE cannot be taken off-line
- The DEFAULT TEMPORARY TABLESPACE cannot be dropped until you create another one.

SQL> SELECT * FROM DATABASE_PROPERTIES where PROPERTY_NAME='DEFAULT_TEMP_TABLESPACE';

Monitoring Temporary Tablespaces and Sorting

Ttempfiles are not listed in V$DATAFILE and DBA_DATA_FILES
Use V$TEMPFILE and DBA_TEMP_FILES.
One can monitor temporary segments from V$SORT_SEGMENT and V$SORT_USAGE
DBA_FREE_SPACE does not record free space for temporary tablespaces. Use V$TEMP_SPACE_HEADER instead:

SQL> select TABLESPACE_NAME, BYTES_USED, BYTES_FREE from V$TEMP_SPACE_HEADER;

TABLESPACE_NAME BYTES_USED BYTES_FREE
------------------------------ ---------- ----------
TEMP 328204288 1819279360
TEMP 332398592 1815085056
TEMP 317718528 1829765120

No comments: