Is it safe to put an index on an Oracle Temporary Table?

EvilTeach picture EvilTeach · Jun 2, 2009 · Viewed 33.9k times · Source

I have read that one should not analyze a temp table, as it screws up the table statistics for others. What about an index? If I put an index on the table for the duration of my program, can other programs using the table be affected by that index?

Does an index affect my process, and all other processes using the table? or Does it affect my process alone?

None of the responses have been authoritative, so I am offering said bribe.

Answer

Quassnoi picture Quassnoi · Jun 8, 2009

Does an index effect my process, and all other processes using the table? or Does it effect my process alone?

I'm assuming we are talking of GLOBAL TEMPORARY tables.

Think of a temporary table as of multiple tables that are created and dropped by each process on the fly from a template stored in the system dictionary.

In Oracle, DML of a temporary table affects all processes, while data contained in the table will affect only one process that uses them.

Data in a temporary table is visible only inside the session scope. It uses TEMPORARY TABLESPACE to store both data and possible indexes.

DML for a temporary table (i. e. its layout, including column names and indexes) is visible to everybody with sufficient privileges.

This means that existence of the index will affect your process as well as other processes using the table in sense that any process that modifies data in the temporary table will also have to modify the index.

Data contained in the table (and in the index too), on the contrary, will affect only the process that created them, and will not even be visible to other processes.

IF you want one process to use the index and another one not to use it, do the following:

  • Create two temporary tables with same column layout
  • Index on one of them
  • Use indexed or non-indexed table depending on the process