05.11.2015 Views

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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CHAPTER 8 ■ TRANSACTIONS 269<br />

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

PL/SQL procedure successfully completed.<br />

I then created a very small undo tablespace <strong>and</strong> altered the system to use it. Note that by<br />

setting AUTOEXTEND off, I have limited the size of all UNDO to be 2MB or less in this system:<br />

ops$tkyte@ORA10G> create undo tablespace undo_small<br />

2 datafile size 2m<br />

3 autoextend off<br />

4 /<br />

Tablespace created.<br />

ops$tkyte@ORA10G> alter system set undo_tablespace = undo_small;<br />

System altered.<br />

Now, with only the small undo tablespace in use, I ran this block of code to do the UPDATE:<br />

ops$tkyte@ORA10G> begin<br />

2 for x in ( select /*+ INDEX(t t_idx) */ rowid rid, object_name, rownum r<br />

3 from t<br />

4 where object_name > ' ' )<br />

5 loop<br />

6 update t<br />

7 set object_name = lower(x.object_name)<br />

8 where rowid = x.rid;<br />

9 if ( mod(x.r,100) = 0 ) then<br />

10 commit;<br />

11 end if;<br />

12 end loop;<br />

13 commit;<br />

14 end;<br />

15 /<br />

begin<br />

*<br />

ERROR at line 1:<br />

ORA-01555: snapshot too old: rollback segment number with name "" too small<br />

ORA-06512: at line 2<br />

I get the error. I should point out that I added an index hint to the query <strong>and</strong> a WHERE<br />

clause to make sure I was reading the table r<strong>and</strong>omly (together, they caused the cost-based<br />

optimizer to read the table “sorted” by the index key). When we process a table via an index,<br />

we will tend to read a block for a single row, <strong>and</strong> then the next row we want will be on a different<br />

block. Ultimately, we will process all of the rows on block 1, just not all at the same time.<br />

Block 1 might hold, say, the data for all rows with OBJECT_NAMEs starting with the letters A, M,<br />

N, Q, <strong>and</strong> Z. So we would hit the block many times, since we are reading the data sorted by<br />

OBJECT_NAME <strong>and</strong> presumably many OBJECT_NAMEs start with letters between A <strong>and</strong> M. Since we<br />

are committing frequently <strong>and</strong> reusing undo space, we eventually revisit a block where we can<br />

simply no longer roll back to the point in time our query began, <strong>and</strong> at that point we get the<br />

error.

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

Saved successfully!

Ooh no, something went wrong!