Using an Oracle temporary table does not generate much redo log as a normal table. However, the undo log is still generated. Thus, how can I write insert, update, or delete statement on a temporary table but Oracle will not generate undo log or generate as little as possible?
Moreover, using /+append/ in the insert statement will generate little undo log. Am I correct? If not, could anyone explain me about using the hint /+append/?
INSERT /*+APPEND*/ INTO table1(...) VALUES(...);
Oracle needs UNDO information to rollback the DML in the transaction. As Gary puts it in his comment:
This UNDO information itself generates REDO. There is nothing you can do about this situation: temporary tables need UNDO and that’s the end of it.
To minimize the amount of UNDO is quite simple: just insert records and select records. INSERT generates the smallest amount of UNDO, because rolling back an INSERT requires simply the rowid. Conversely DELETE statements generate the most UNDO, because the database has to store the entire record. Basically, to rollback an INSERT issue a DELETE, to rollback a DELETE issue an INSERT. An UPDATE generates a variable amount of UNDO, because we need the old versions of the changed columns; the more columns changed and the bigger they are, the larger the amount of UNDO generated.
Demonstration
In session one a user will insert a lot of records into a temporary table and then delete them. In session two a DBA will monitor the transaction’s UNDO usage.
Undo usage:
Now the deletion:
Undo usage (several samples during a long running statement)::
Commit (the temporary table has transaction scope i.e. DELETE ROWS)
Undo usage:
The undo usage is accumulative:
Undo usage
Summary
So, to minimise the impact of UNDO which a temporary table generates make sure you insert the right data, once. Avoid applying updates to it and especially avoid deleting large numbers of records from them. If you are using a temporary table with a transaction scope there really should be no need to delete records from it. If your temporary table has a session duration and you need to clear it out, it would be better to use TRUNCATE, if possible, rather than DELETE.