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.

330<br />

CHAPTER 9 ■ REDO AND UNDO<br />

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

2 as<br />

3 select *<br />

4 from all_objects<br />

5 order by dbms_r<strong>and</strong>om.r<strong>and</strong>om;<br />

Table created.<br />

ops$tkyte@ORA10G> alter table t add constraint t_pk primary key(object_id)<br />

2 /<br />

Table altered.<br />

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

PL/SQL procedure successfully completed.<br />

And now we are ready to do our modifications:<br />

ops$tkyte@ORA10G> begin<br />

2 for x in ( select rowid rid from t )<br />

3 loop<br />

4 update t set object_name = lower(object_name) where rowid = x.rid;<br />

5 commit;<br />

6 end loop;<br />

7 end;<br />

8 /<br />

Now, while that is running, we run a query in another session. This query was reading this<br />

table T <strong>and</strong> processing each record. I spent about 1/100 of a second processing each record<br />

before fetching the next (simulated using DBMS_LOCK.SLEEP(0.01)). I used the FIRST_ROWS hint<br />

in the query to have it use the index we created to read the rows out of the table via the index<br />

sorted by OBJECT_ID. Since the data was r<strong>and</strong>omly inserted into the table, we would tend to<br />

query blocks in the table rather r<strong>and</strong>omly. This block ran for only a couple of seconds before<br />

failing:<br />

ops$tkyte@ORA10G> declare<br />

2 cursor c is<br />

3 select /*+ first_rows */ object_name<br />

4 from t<br />

5 order by object_id;<br />

6<br />

7 l_object_name t.object_name%type;<br />

8 l_rowcnt number := 0;<br />

9 begin<br />

10 open c;<br />

11 loop<br />

12 fetch c into l_object_name;<br />

13 exit when c%notfound;<br />

14 dbms_lock.sleep( 0.01 );<br />

15 l_rowcnt := l_rowcnt+1;<br />

16 end loop;

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

Saved successfully!

Ooh no, something went wrong!