05.11.2015 Views

Apress.Expert.Oracle.Database.Architecture.9i.and.10g.Programming.Techniques.and.Solutions.Sep.2005

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

320<br />

CHAPTER 9 ■ REDO AND UNDO<br />

back to a SAVEPOINT within a transaction. You might erase the last 50 INSERTs into a temporary<br />

table, leaving the first 50. Temporary tables can have constraints <strong>and</strong> everything else a normal<br />

table can have. They might fail a statement on the five-hundredth row of a 500-row INSERT,<br />

necessitating a rollback of that statement. Since temporary tables behave in general just like<br />

“normal” tables, temporary tables must generate undo. Since undo data must be logged, temporary<br />

tables will generate some redo log for the undo they generate.<br />

This is not nearly as ominous as it seems. The primary SQL statements used against temporary<br />

tables are INSERTs <strong>and</strong> SELECTs. Fortunately, INSERTs generate very little undo (you need<br />

to restore the block to “nothing,” <strong>and</strong> it doesn’t take very much room to store “nothing”), <strong>and</strong><br />

SELECTs generate no undo. Hence, if you use temporary tables for INSERTs <strong>and</strong> SELECTs exclusively,<br />

this section means nothing to you. It is only if you UPDATE or DELETE that you might be<br />

concerned about this.<br />

I set up a small test to demonstrate the amount of redo generated while working with<br />

temporary tables, an indication therefore of the amount of undo generated for temporary<br />

tables, since only the undo is logged for them. To demonstrate this, I will take identically configured<br />

“permanent” <strong>and</strong> “temporary” tables, <strong>and</strong> then perform the same operations on each,<br />

measuring the amount of redo generated each time. The tables I used were simply as follows:<br />

ops$tkyte@ORA10G> create table perm<br />

2 ( x char(2000) ,<br />

3 y char(2000) ,<br />

4 z char(2000) )<br />

5 /<br />

Table created.<br />

ops$tkyte@ORA10G> create global temporary table temp<br />

2 ( x char(2000) ,<br />

3 y char(2000) ,<br />

4 z char(2000) )<br />

5 on commit preserve rows<br />

6 /<br />

Table created.<br />

I set up a small stored procedure to allow me to perform arbitrary SQL <strong>and</strong> report the<br />

amount of redo generated by that SQL. I will use this routine to perform INSERTs, UPDATEs, <strong>and</strong><br />

DELETEs against both the temporary <strong>and</strong> permanent tables:<br />

ops$tkyte@ORA10G> create or replace procedure do_sql( p_sql in varchar2 )<br />

2 as<br />

3 l_start_redo number;<br />

4 l_redo number;<br />

5 begin<br />

6 select v$mystat.value<br />

7 into l_start_redo<br />

8 from v$mystat, v$statname<br />

9 where v$mystat.statistic# = v$statname.statistic#<br />

10 <strong>and</strong> v$statname.name = 'redo size';<br />

11<br />

12 execute immediate p_sql;

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!