Sunday, 10 December 2023

Temporary Tables

The data in a global temporary table is private, such that data inserted by a session can only be accessed by that session. The session-specific rows in a global temporary table can be preserved for the whole session, or just for the current transaction.

The ON COMMIT DELETE ROWS clause indicates the data should be deleted at the end of the transaction.

In contrast, the ON COMMIT PRESERVE ROWS clause indicates that rows should persist beyond the end of the transaction. They will only be removed at the end of the session.


CREATE GLOBAL TEMPORARY TABLE my_temp_table (
  id           NUMBER,
  description  VARCHAR2(20)

ON COMMIT PRESERVE ROWS;

Although the data in a GTT is written to the temporary tablespace, the associated undo is still written to the normal undo tablespace, which is itself protected by redo, so using a GTT does not reduce undo and the redo associated with protecting the undo tablespace.

-- Check undo used by transaction.
SELECT t.used_ublk,       t.used_urec
FROM   v$transaction t,
       v$session s
WHERE  s.saddr = t.ses_addr
AND    s.audsid = SYS_CONTEXT('USERENV', 'SESSIONID');

Ref:- https://oracle-base.com/articles/misc/temporary-tables

Oracle 12c introduced the concept of Temporary Undo, allowing  the undo segments for global temporary tables to be stored in the temporary tablespace , thereby preventing the need to create redo.

This feature is available using the Temp_undo_Enabled parameter at session or system level.
Refer-- https://oracle-base.com/articles/12c/temporary-undo-12cr1

A new variation of temporary tables has been introduced in Oracle 18c. A private temporary table is a memory-based temporary table that is dropped at the end of the session or transaction depending on the setup . It uses the prefix ora$ptt_ .

CREATE PRIVATE TEMPORARY TABLE ora$ptt_my_temp_table (
  id           NUMBER,
  description  VARCHAR2(20)
)
ON COMMIT DROP DEFINITION;

No comments:

Post a Comment