Managing Tablespaces   «Prev  Next»
Lesson 9 Temporary tablespaces
Objective Use temporary tablespaces for sorting data.

Temporary tablespaces

In Oracle databases, temporary tablespaces are a specialized type of tablespace designed to handle transient data, such as that used in sorting operations during query execution. Utilizing temporary tablespaces for sorting data is a critical aspect of optimizing database performance and resource management. This approach has several advantages, including reducing I/O load on regular tablespaces, isolating sorting operations, and improving the efficiency of large queries.

Theory and Application of Temporary Tablespaces for Sorting

  1. Purpose of Temporary Tablespaces:
    • Temporary tablespaces are intended for temporary, non-persistent data that is generated during database operations, particularly sorting and hash operations. These tablespaces are not meant for storing permanent data and are cleared when the database session ends.
  2. Sorting Operations:
    • Sorting operations often occur when executing SQL queries that involve `ORDER BY`, `GROUP BY`, or aggregation functions. If the data to be sorted does not fit into memory (i.e., the PGA or Program Global Area), Oracle uses temporary tablespaces to perform these sorts on disk.
  3. Managing Disk I/O:
    • By using a temporary tablespace for sorting, the database minimizes disk I/O on the primary tablespaces. This isolation helps maintain the performance of regular database operations, as sorting can be I/O intensive.

Implementing Sorting in Temporary Tablespaces

  1. Creating a Temporary Tablespace:
    • The first step is to create a temporary tablespace using the `CREATE TEMPORARY TABLESPACE` command. This command allows specification of attributes such as datafile size and location.
    • Example: `CREATE TEMPORARY TABLESPACE temp_ts TEMPFILE 'temp01.dbf' SIZE 500M;`
  2. Assigning Temporary Tablespace to Users:
    • Assign the temporary tablespace to database users who require sorting operations. This is done using the `ALTER USER` command.
    • Example: `ALTER USER user_name TEMPORARY TABLESPACE temp_ts;`
    • This assignment directs the user's sorting activities to the specified temporary tablespace.
  3. Configuring Sort Area Size:
    • The efficiency of sorting operations can be influenced by configuring parameters such as `SORT_AREA_SIZE` (for manual memory management) or PGA settings (for automatic memory management). These settings determine how much data is sorted in memory before it spills over to the temporary tablespace.
  4. Monitoring Temporary Tablespace Usage:
    • Regular monitoring of the temporary tablespace is essential to ensure it is adequately sized and performing efficiently. Oracle provides data dictionary views like `DBA_TEMP_FREE_SPACE` for monitoring the usage of temporary tablespaces.
  5. Temporary Tablespace Groups:
    • For large databases, using temporary tablespace groups can enhance sorting performance. A temporary tablespace group is a collection of temporary tablespaces, allowing multiple tablespaces to be used for a single sorting operation. This approach can distribute the I/O load and improve efficiency.
  6. Best Practices:
    • Avoid placing temporary tablespaces on the same physical disk as the datafiles of regular tablespaces to prevent I/O contention.
    • Size temporary tablespaces appropriately to handle the expected load, considering the nature of the queries and the data volume.

In conclusion, effectively utilizing temporary tablespaces for sorting operations in Oracle databases is a strategic approach to optimize query performance and resource utilization. It involves careful planning in terms of tablespace creation, user assignment, and configuration of memory parameters, coupled with ongoing monitoring and management to ensure efficient database operations.
You can improve database performance by doing sorting in temporary tablespaces instead of permanent ones. You can flag a tablespace as temporary by using the TEMPORARY keyword when you create the tablespace. You can also use the ALTER TABLESPACE statement to convert a tablespace into a temporary tablespace. Here are some examples:

CREATE TABLESPACE coin_sort
   DATAFILE 'e:\oracle\oradata\coin\coin_sort01.dat' SIZE 10k
   TEMPORARY;
   
ALTER TABLESPACE coin_sort TEMPORARY;

Creates tablespace as a temporary tablespace

The first statement creates a tablespace as a temporary tablespace. The second statement alters an existing tablespace, making it temporary. The keyword PERMANENT may be used with the ALTER TABLESPACE statement to reverse the process and to change a tablespace from temporary to permanent. Once you have a temporary tablespace, you can assign users to that tablespace by using the TEMPORARY TABLESPACE clause with the CREATE USER and ALTER USER statements. For example:
ALTER USER coin_admin
TEMPORARY TABLESPACE coin_sort;

Oracle uses temporary tablespaces only for temporary objects such as sort segments. For more efficient operation, Oracle allocates sort segments to an instance, rather than to individual users. Sorts are often done in response to queries containing ORDER BY, GROUP BY, and DISTINCT clauses. By assigning users to temporary tablespaces, you ensure that those sorts are done as efficiently as possible. In the next lesson, you will learn how temporary tablespaces appear in the data dictionary.