Search this Blog

Tuesday, May 15, 2012

Global Temporary Tables


Triggered by the huge amount of archive files generated by several databases each night, we have been looking for a solution to reduce this dramatically.
This solution exists !!
And it is called “Global Temporary Tables”.

The definition:

 Applications often use some form of temporary data store for processes that are too complicated to complete in a single pass. Often, these temporary stores are defined as database tables or PL/SQL tables. As of Oracle 8i , the maintenance and management of temporary tables can be delegated to the server by using 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.
Finally the data stored in global temporary tables generate NO REDO and thus reduce the amount of ARCHIVE FILES.

The usage

Above, three words are marked in bold. These words indicate the behavior of a temporary table. Although a global temporary table can exists in a global schema, like EHDA_KRG, all data inserted into that table is session-specific. This means that if more than one user session is inserting/updating data in a global temporary table, only the data of that user’s session is affected. So, although you’re working in the same temporary table you’re working on your own data. This makes the table private.
Furthermore there are two major differences in behavior. Default the table only keeps the data inserted/updated until the transaction is finished/committed. This is known as “ON COMMIT DELETE ROWS”.
The other behavior keeps all rows/changes in the table until the end of your session. This behavior is known as “ON COMMIT PRESERVE ROWS”. If you want the temporary table to behave this way, you have to include this command in the table create statement.

The creation of a global temporary table, that preserves the data until the session is ended, looks like:

CREATE GLOBAL TEMPORARY TABLE
   SELECT * FROM .....
ON COMMIT PRESERVE ROWS
AS .......

or

CREATE GLOBAL TEMPORARY TABLE
( )
ON COMMIT PRESERVE ROWS ;

And the creation of a global temporary table, that preserves the data only within a transaction, looks like:

CREATE GLOBAL TEMPORARY TABLE
ON COMMIT DELETE ROWS
AS
   SELECT * FROM .....
or

CREATE GLOBAL TEMPORARY TABLE
( )
ON COMMIT DELETE ROWS ;

Some other 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 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 that same as the database session. Indexes can however only be created on empty tables !!
  • 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.
  • A temporary table can only be dropped if all users that have data in it, have issued the 'truncate table' command against it, or disconnected their session. Otherwise an ORA-14452 is generated.



No comments:

Post a Comment