UnderStanding Global Temporary Table in Oracle Database


In Oracle, Global Temporary Tables (GTTs) are a special type of table designed to hold data that is session-specific or transaction-specific. Unlike permanent tables, the data in a global temporary table exists only for the duration of a session or a transaction. This makes GTTs a great option when you need to store intermediate data, temporary results, or perform operations that require temporary data storage without impacting the permanent data.

This article provides an in-depth look at GTTs, how they work, their use cases, and technical details about how they are implemented in Oracle.

Key Features of Global Temporary Tables

  1. Session or Transaction-Specific Data:

    • Data inserted into a GTT is visible only to the session that inserted it. Other sessions cannot access this data.
    • The data can either be retained for the entire duration of the session or only until the transaction ends, depending on the table definition.
  2. No Permanent Data Storage:

    • Data in a GTT is not stored permanently on disk. It is purged either when the transaction completes or the session ends.
    • The table structure (definition) is permanent, but the data is always temporary.
  3. No Logging of DML Operations:

    • DML operations on GTTs are not logged in Oracle's redo logs. This can lead to performance improvements, as Oracle avoids generating undo and redo for temporary data.
  4. Reduced Contention:

    • Since each session has its own copy of the data, there is no contention for data between different users. This makes GTTs highly efficient in multi-user environments.
  5. Temporary Segments:

    • Oracle uses temporary segments to store the data of a GTT. These are cleaned up automatically when the session or transaction ends, depending on the settings.

Creating a Global Temporary Table

To create a GTT in Oracle, you use the CREATE GLOBAL TEMPORARY TABLE statement. Below is an example:


CREATE GLOBAL TEMPORARY TABLE temp_sales_data ( sales_id NUMBER, product_id NUMBER, quantity NUMBER, sale_date DATE ) ON COMMIT DELETE ROWS;


CREATE GLOBAL TEMPORARY TABLE temp_sales_data ( sales_id NUMBER, product_id NUMBER, quantity NUMBER, sale_date DATE ) ON COMMIT PRESERVE ROWS;


In this example:

  • ON COMMIT DELETE ROWS: This option specifies that the data will be deleted at the end of the transaction.
  • ON COMMIT PRESERVE ROWS: Alternatively, you can use this option to preserve data until the session ends, even after a commit.

How Data is Managed

  1. Session-specific Data: When you use the ON COMMIT PRESERVE ROWS clause, the data is available for the entire duration of the session. Even if you issue a COMMIT, the data will still persist until the session ends.

  2. Transaction-specific Data: With the ON COMMIT DELETE ROWS clause, the data is transaction-specific. This means the data will be deleted as soon as the transaction is committed or rolled back.

Example Usage Scenarios

  1. Reporting: GTTs are ideal when generating complex reports that involve multiple steps of data transformation. You can use GTTs to store intermediate results without worrying about permanently storing the data or impacting the permanent database tables.

  2. Batch Processing: When running batch processes, such as updating or processing a large number of records, GTTs can be used to store temporary data during the operation. Once the batch is completed, the temporary data is automatically removed.

  3. ETL (Extract, Transform, Load) Operations: GTTs can be helpful in ETL processes where temporary staging tables are required to store data before it is transformed and loaded into the final destination tables.

  4. Complex Joins: If a query requires complex joins across large datasets, it can be beneficial to break the query into smaller parts and store intermediate results in a GTT. This can improve performance by reducing the complexity of individual queries.

Best Practices

  1. Avoid Indexing Temporary Tables:

    • Since GTTs are primarily used for temporary data, creating indexes on GTTs can degrade performance due to the overhead of maintaining the index. Use indexes only if you need them to speed up specific queries.
  2. Use Appropriate ON COMMIT Clause:

    • Choose the correct ON COMMIT clause based on your use case. If the data is required across transactions, use ON COMMIT PRESERVE ROWS. Otherwise, use ON COMMIT DELETE ROWS to automatically clean up the data after each transaction.
  3. Leverage GTTs for Bulk Operations:

    • When dealing with bulk inserts, updates, or deletes, GTTs can significantly improve performance by reducing contention and logging.
  4. Watch Out for Space Usage:

    • While GTTs do not store data permanently, they still use temporary segments that occupy space in the temporary tablespace. Ensure that your temporary tablespace has sufficient space to handle the data.

Example: Using Global Temporary Table in a Procedure

Here’s a sample PL/SQL procedure that uses a GTT to store and process temporary data:


CREATE OR REPLACE PROCEDURE process_sales_data IS BEGIN -- Insert data into the GTT INSERT INTO temp_sales_data (sales_id, product_id, quantity, sale_date) SELECT sales_id, product_id, quantity, sale_date FROM sales WHERE sale_date = SYSDATE; -- Process the temporary data UPDATE temp_sales_data SET quantity = quantity * 2 WHERE product_id IN (SELECT product_id FROM discounts); -- Perform further operations... -- Data will be automatically removed at the end of the transaction. COMMIT; END; /

In this procedure, temporary data is inserted into the temp_sales_data table, processed, and committed. The data is cleaned up automatically because ON COMMIT DELETE ROWS is used in the GTT definition.

Performance Considerations

  1. Memory and Disk Usage: Although GTTs do not store data permanently, the data inserted into them is stored in the Temporary Tablespace. If a session inserts large amounts of data into a GTT, this can consume significant amounts of temporary space.

  2. Undo and Redo Logging: One of the advantages of GTTs is that operations on these tables generate minimal redo and undo logs, leading to better performance. However, some undo information may still be generated depending on the operation and the isolation level of the session.

  3. Parallel DML: Parallel DML (Data Manipulation Language) operations are not supported on GTTs, as each session has its own private copy of the data.

Global Temporary Tables are a powerful feature in Oracle that allows users to store session- or transaction-specific data temporarily. They improve performance in scenarios where intermediate data needs to be processed without being permanently stored. By following best practices and understanding the nuances of GTTs, you can optimize performance and efficiently manage temporary data in your Oracle database applications.









Please do like and subscribe to my youtube channel: https://www.youtube.com/@foalabs If you like this post please follow,share and comment