Global Temporary Table

What is Global temporary table? and what are the benefits of it?

Questions by ravishing_sid

Showing Answers 1 - 10 of 10 Answers

GLOBAL TEMPORARY TABLE statement defines a temporary table for the current session. The declared temporary table description does not appear in the system catalog. It is not persistent and cannot be shared with other sessions. Each session that defines a declared global temporary table of the same name has its own unique description of the temporary table. When the session terminates, the rows of the table are deleted, and the description of the temporary table is dropped.

taya_ronak

  • Nov 3rd, 2008
 

Global temporary table is kind of temporary table managed by system. it'll keep in temporary tablespace till the session remain. there are two methods available i.e.

1) create global temporary table t1 on commit delete rows.
with this option, table t1 data will be lost whenever commit will be issued by user.

2) create global temporary table t1 on commit preserve rows.
with this option, table t1 data will be lost whenever session is lost.

after deletion we do not need to give any storage management command because global temporary tables are system managed and after deletion of data, space which was occupied by table are ready to allocate for other use.

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.

CREATE GLOBAL TEMPORARY TABLE my_temp_table ( column1 NUMBER, column2 NUMBER ) ON COMMIT DELETE ROWS;

In contrast, the ON COMMIT PRESERVE ROWS clause indicates that rows should be preserved until the end of the session.

CREATE GLOBAL TEMPORARY TABLE my_temp_table ( column1 NUMBER, column2 NUMBER ) ON COMMIT PRESERVE ROWS;

Miscellaneous Features

  • If the TRUNCATE statement is issued against a temporary table, only the session specific data is trucated. There is no affect on the data of other sessions.
  • Data in temporary tables is automatically delete 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 that 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.
  • There are a number of restrictions related to temporary tables but these are version specific.

  Was this answer useful?  Yes

Global Temporary Table(GTT):
---------------------------------------
--> GTT operation perform in-memory(RAM) so reading data from GTT is a "Logical Read" -> hence its faster.
--> GTT are of 2 types:
1. Session Specific GTT (.. ON COMMIT PRESERVE ROWS .. )
2. Transaction Specific GTT (.. ON COMMIT DELETE ROWS .. )
--> Session Specific GTT: It will preserve the rows till the session exists.
--> Transaction Specific GTT: It will delete the rows once the transaction complete(i.e. commit/rollback)

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions