Worldscope

Oracle Global Temporary tables

Palavras-chave:

Publicado em: 03/08/2025

Oracle Global Temporary Tables: A Comprehensive Guide

Oracle Global Temporary Tables (GTTs) provide a way to store temporary data within a database session. Unlike regular tables, the data in a GTT is private to each session and is automatically purged when the session ends or when a transaction commits, depending on the GTT's definition. This article provides a comprehensive guide to using GTTs, including their creation, usage, and benefits.

Fundamental Concepts / Prerequisites

Before diving into GTTs, it's beneficial to understand the following concepts:

  • Database Sessions: A database session is a logical connection between a client application and the Oracle database.
  • Transactions: A transaction is a sequence of database operations treated as a single logical unit of work.
  • Data Definition Language (DDL): DDL statements are used to define database objects like tables.
  • Data Manipulation Language (DML): DML statements are used to manipulate data within database objects like tables (e.g., INSERT, UPDATE, DELETE).

Core Implementation/Solution

The core of using GTTs involves creating the table structure and then manipulating the data within that structure during a session. Here's how to do it:


-- Create a Global Temporary Table
CREATE GLOBAL TEMPORARY TABLE temp_employee (
    employee_id NUMBER,
    employee_name VARCHAR2(100),
    department VARCHAR2(50)
)
ON COMMIT DELETE ROWS;

-- Alternatively, use ON COMMIT PRESERVE ROWS to persist the data until the session ends
-- CREATE GLOBAL TEMPORARY TABLE temp_employee (
--     employee_id NUMBER,
--     employee_name VARCHAR2(100),
--     department VARCHAR2(50)
-- )
-- ON COMMIT PRESERVE ROWS;

-- Insert data into the Global Temporary Table (within a session)
INSERT INTO temp_employee (employee_id, employee_name, department)
VALUES (1, 'John Doe', 'IT');

INSERT INTO temp_employee (employee_id, employee_name, department)
VALUES (2, 'Jane Smith', 'HR');

-- Query the Global Temporary Table (within the same session)
SELECT * FROM temp_employee;

-- Commit the transaction (only relevant if ON COMMIT DELETE ROWS is used)
COMMIT;

-- After the commit (with ON COMMIT DELETE ROWS), the table is empty
SELECT * FROM temp_employee;

-- In a new session, the table is empty
-- SELECT * FROM temp_employee; -- Result: No rows selected

Code Explanation

The SQL code demonstrates the creation and usage of a Global Temporary Table. Let's break it down:

`CREATE GLOBAL TEMPORARY TABLE temp_employee (...)`: This statement creates the GTT named `temp_employee`. The table structure is defined with columns `employee_id`, `employee_name`, and `department` along with their respective data types. The `GLOBAL` keyword specifies that it's a global temporary table.

`ON COMMIT DELETE ROWS;`: This clause determines the behavior of the GTT's data when a transaction is committed. `DELETE ROWS` means that all rows in the table are automatically deleted when a `COMMIT` statement is executed. Alternatively, `ON COMMIT PRESERVE ROWS` can be used to retain data until the session ends.

`INSERT INTO temp_employee (...) VALUES (...)`: These statements insert data into the `temp_employee` table. The data is only visible within the current session.

`SELECT * FROM temp_employee;`: This query retrieves all data from the `temp_employee` table. The result will reflect the data inserted within the current session.

`COMMIT;`: This statement commits the current transaction. If `ON COMMIT DELETE ROWS` is specified in GTT's definition, all rows in the GTT are deleted at the point of committing.

Complexity Analysis

The complexity of operations on a Global Temporary Table largely depends on the operations performed on the table itself, not the fact that it is temporary. However, it's important to understand the session-specific nature for a comprehensive analysis.

Time Complexity:

  • `CREATE GLOBAL TEMPORARY TABLE`: This operation has a relatively low time complexity, typically O(1), as it primarily involves creating metadata rather than manipulating large amounts of data.
  • `INSERT`, `UPDATE`, `DELETE`: These DML operations have similar complexities to standard table operations, generally ranging from O(1) (for indexed lookups) to O(n) (for full table scans) depending on indexes and the volume of data.
  • `SELECT`: The complexity of a SELECT statement is dependent on whether the table contains indexes or not. With indexes it can be O(log n), without, it can be O(n).

Space Complexity:

  • The space occupied by a GTT depends on the data inserted into it within each session. The table definition is stored once globally, but the data itself is stored in temporary segments (typically in the user's temporary tablespace) for each session. Therefore, the space complexity is O(m*k) where 'm' is the maximum size of the data for a single session, and 'k' is the number of concurrent sessions accessing the table. The space is released upon `COMMIT` if `ON COMMIT DELETE ROWS` is used or when the session ends if `ON COMMIT PRESERVE ROWS` is used.

Alternative Approaches

While GTTs are useful, there are alternative approaches to managing temporary data in Oracle:

Regular Tables with Truncation: A regular table can be created, used, and then truncated after use within a session. This approach involves creating and maintaining a regular table and manually clearing the data. The drawback is the need for more explicit cleanup, and increased chance for data leakage if cleanup is forgotten. This approach can also introduce locking contention if multiple sessions are trying to truncate the same table.

Conclusion

Oracle Global Temporary Tables provide a convenient and efficient way to manage session-specific temporary data. The key takeaways are understanding how to create GTTs with the appropriate `ON COMMIT` option and realizing that the data's lifecycle is tied to the session or transaction, depending on the configuration. By using GTTs effectively, developers can simplify their code, improve performance, and ensure data privacy.