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.

324<br />

CHAPTER 9 ■ REDO AND UNDO<br />

ops$tkyte@ORA10G> create index t_idx on t(indexed);<br />

Index created.<br />

ops$tkyte@ORA10G> exec dbms_stats.gather_table_stats(user,'T');<br />

PL/SQL procedure successfully completed.<br />

Now we’ll update the table, first updating the unindexed column <strong>and</strong> then the indexed<br />

column. We’ll need a new V$ query to measure the amount of undo we’ve generated in each<br />

case. The following query accomplishes this for us. It works by getting our session ID (SID)<br />

from V$MYSTAT, using that to find our record in the V$SESSION view, <strong>and</strong> retrieving the transaction<br />

address (TADDR). It uses the TADDR to pull up our V$TRANSACTION record (if any) <strong>and</strong><br />

selects the USED_UBLK column—the number of used undo blocks. Since we currently are not<br />

in a transaction, the query is expected to return 0 rows right now:<br />

ops$tkyte@ORA10G> select used_ublk<br />

2 from v$transaction<br />

3 where addr = (select taddr<br />

4 from v$session<br />

5 where sid = (select sid<br />

6 from v$mystat<br />

7 where rownum = 1<br />

8 )<br />

9 )<br />

10 /<br />

no rows selected<br />

This is the query I’ll use after each UPDATE, but I won’t repeat it in the text—I’ll just show the<br />

results.<br />

Now we are ready to perform the updates <strong>and</strong> measure the number of undo blocks used<br />

by each:<br />

ops$tkyte@ORA10G> update t set unindexed = lower(unindexed);<br />

48771 rows updated.<br />

ops$tkyte@ORA10G> select used_ublk<br />

...<br />

10 /<br />

USED_UBLK<br />

----------<br />

401<br />

ops$tkyte@ORA10G> commit;<br />

Commit complete.<br />

That UPDATE used 401 blocks to store its undo. The commit would “free” that up, or release it,<br />

so if we rerun the query against V$TRANSACTION it would once again show us no rows selected.<br />

When we update the same data—only indexed this time—we’ll observe the following:

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

Saved successfully!

Ooh no, something went wrong!