Global Temporary Tables (GTT)
Applications often use some form of temporary data store for processes that are to complicated to complete in a single pass. Often, these temporary stores are defined as database tables or PL/SQL tables. From Oracle 8i onward, the maintenance and management of temporary tables can be delegated to the server by using Global Temporary Tables.
- Creation of Global Temporary Tables
- Global Temporary Tables and Undo
- Global Temporary Tables and Redo
- Miscellaneous Features
Related articles.
- Temporary Undo in Oracle Database 12c Release 1 (12.1)
- Session Sequences in Oracle Database 12c Release 1 (12.1)
- Session-Private Statistics for Global Temporary Tables in Oracle Database 12c Release 1 (12.1)
- Temporary Undo for Global Temporary Tables (GTTs) in Oracle 12c
- Global Temporary Tables (GTT) in Oracle
Creation of Global 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 that the data should be deleted at the end of the transaction, or the end of the session.CREATE GLOBAL TEMPORARY TABLE my_temp_table (
id NUMBER,
description VARCHAR2(20)
)
ON COMMIT DELETE ROWS;
-- Insert, but don't commit, then check contents of GTT.
INSERT INTO my_temp_table VALUES (1, 'ONE');
SELECT COUNT(*) FROM my_temp_table;
COUNT(*)
----------
1
SQL>
-- Commit and check contents.
COMMIT;
SELECT COUNT(*) FROM my_temp_table;
COUNT(*)
----------
0
SQL>
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;
-- Insert and commit, then check contents of GTT.
INSERT INTO my_temp_table VALUES (1, 'ONE');
COMMIT;
SELECT COUNT(*) FROM my_temp_table;
COUNT(*)
----------
1
SQL>
-- Reconnect and check contents of GTT.
CONN test/test
SELECT COUNT(*) FROM my_temp_table;
COUNT(*)
----------
0
SQL>
Global Temporary Tables and Undo
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.
The following code creates a conventional table, populates it and checks the amount of undo used by the transaction.
DROP TABLE my_temp_table PURGE;
-- Create conventional table.
CREATE TABLE my_temp_table (
id NUMBER,
description VARCHAR2(20)
);
-- Populate table.
INSERT INTO my_temp_table
WITH data AS (
SELECT 1 AS id
FROM dual
CONNECT BY level < 10000
)
SELECT rownum, TO_CHAR(rownum)
FROM data a, data b
WHERE rownum <= 1000000;
-- 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');
USED_UBLK USED_UREC
---------- ----------
302 6237
SQL>
We now repeat the previous test, but this time using a GTT.
DROP TABLE my_temp_table PURGE;
-- Create GTT.
CREATE GLOBAL TEMPORARY TABLE my_temp_table (
id NUMBER,
description VARCHAR2(20)
)
ON COMMIT PRESERVE ROWS;
-- Populate GTT.
INSERT INTO my_temp_table
WITH data AS (
SELECT 1 AS id
FROM dual
CONNECT BY level < 10000
)
SELECT rownum, TO_CHAR(rownum)
FROM data a, data b
WHERE rownum <= 1000000;
-- 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');
USED_UBLK USED_UREC
---------- ----------
303 6238
SQL>
TRUNCATE TABLE my_temp_table;
We can see, there is no significant difference in the undo used.
Oracle 12c introduced the concept of Temporary Undo, allowing the undo for a GTT to be written to the temporary tablespace, thereby reducing undo and redo.
Global Temporary Tables and Redo
If you've read the previous section, you will already know the relationship between global temporary tables and redo. The data in a GTT is written to the temporary tablespace, which is not directly protected by redo, so using a GTT improves performance by reducing redo generation. Unfortunately, prior to Oracle 12c, all undo associated with DML against a GTT is written to the normal undo tablespace, which is itself protected by redo. As a result, using a GTT reduces the amount of redo generation, but does not eliminate it. Another why of describing this is, using a GTT removes direct redo generation, but not indirect redo generation cause by undo.
The following code creates a conventional table, populates it and checks the amount of redo generated by the transaction.
DROP TABLE my_temp_table PURGE;
-- Create conventional table.
CREATE TABLE my_temp_table (
id NUMBER,
description VARCHAR2(20)
);
SET AUTOTRACE ON STATISTICS;
-- Populate table.
INSERT INTO my_temp_table
WITH data AS (
SELECT 1 AS id
FROM dual
CONNECT BY level < 10000
)
SELECT rownum, TO_CHAR(rownum)
FROM data a, data b
WHERE rownum <= 1000000;
1000000 rows created.
Statistics
----------------------------------------------------------
106 recursive calls
20119 db block gets
2603 consistent gets
16 physical reads
23039396 redo size
853 bytes sent via SQL*Net to client
987 bytes received via SQL*Net from client
3 SQL*Net roundtrips to/from client
6 sorts (memory)
0 sorts (disk)
1000000 rows processed
SQL>
We now repeat the previous test, but this time using a GTT.
DROP TABLE my_temp_table PURGE;
-- Create GTT.
CREATE GLOBAL TEMPORARY TABLE my_temp_table (
id NUMBER,
description VARCHAR2(20)
)
ON COMMIT PRESERVE ROWS;
SET AUTOTRACE ON STATISTICS;
-- Populate GTT.
INSERT INTO my_temp_table
WITH data AS (
SELECT 1 AS id
FROM dual
CONNECT BY level < 10000
)
SELECT rownum, TO_CHAR(rownum)
FROM data a, data b
WHERE rownum <= 1000000;
1000000 rows created.
Statistics
----------------------------------------------------------
45 recursive calls
15333 db block gets
2381 consistent gets
16 physical reads
2944180 redo size
862 bytes sent via SQL*Net to client
987 bytes received via SQL*Net from client
3 SQL*Net roundtrips to/from client
5 sorts (memory)
0 sorts (disk)
1000000 rows processed
SQL>
TRUNCATE TABLE my_temp_table;
We can see we have created an order of magnitude less redo when using the GTT, but we have not eliminated it.
Miscellaneous Features
- If the TRUNCATE statement is issued against a temporary table, only the session specific data is truncated. There is no affect on the data of other sessions.
- Data in temporary tables is stored in temp segments in the temp tablespace.
- Data in temporary tables is automatically deleted at the end of the database session, even if it ends abnormally.
- Indexes can be created on temporary tables. The content of the index and the scope of the index is the same as the database session.
- Views can be created against temporary tables and combinations of temporary and permanent tables.
- Temporary tables can have triggers associated with them.
- Export and Import utilities can be used to transfer the table definitions, but no data rows are processed.
- Statistics on temporary tables are common to all sessions. Oracle 12c allows session specific statistics.
- There are a number of restrictions related to temporary tables but these are version specific.
For more information see:
- Temporary Tables
- CREATE TABLE - GLOBAL TEMPORARY
- Temporary Undo in Oracle Database 12c Release 1 (12.1)
- Session Sequences in Oracle Database 12c Release 1 (12.1)
- Session-Private Statistics for Global Temporary Tables in Oracle Database 12c Release 1 (12.1)
- Temporary Undo for Global Temporary Tables (GTTs) in Oracle 12c
- Global Temporary Tables (GTT) in Oracle
Hope this helps. Regards Tim...
No comments:
Post a Comment