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.

382<br />

CHAPTER 10 ■ DATABASE TABLES<br />

ops$tkyte@ORA10GR1> begin<br />

2 dbms_stats.gather_table_stats( user, 'T_HEAP', cascade=>true );<br />

3 end;<br />

4 /<br />

PL/SQL procedure successfully completed.<br />

Now, all I needed was some “r<strong>and</strong>om” data to pick rows from each of the tables with. To<br />

achieve that, I simply selected all of the OBJECT_IDs into an array <strong>and</strong> had them sorted r<strong>and</strong>omly,<br />

to hit the table all over in a scattered fashion. I used a PL/SQL package to define <strong>and</strong><br />

declare the array <strong>and</strong> a bit of PL/SQL code to “prime” the array, to fill it up:<br />

ops$tkyte@ORA10GR1> create or replace package state_pkg<br />

2 as<br />

3 type array is table of t_hashed.object_id%type;<br />

4 g_data array;<br />

5 end;<br />

6 /<br />

Package created.<br />

ops$tkyte@ORA10GR1> begin<br />

2 select object_id bulk collect into state_pkg.g_data<br />

3 from t_hashed<br />

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

5 end;<br />

6 /<br />

PL/SQL procedure successfully completed.<br />

To see the work performed by each, I used the following block of code (if you replace<br />

occurrences of the word HASHED with HEAP, you have the other block of code you need to test<br />

against):<br />

ops$tkyte@ORA10GR1> declare<br />

2 l_rec t_hashed%rowtype;<br />

3 begin<br />

4 for i in 1 .. state_pkg.g_data.count<br />

5 loop<br />

6 select * into l_rec from t_hashed<br />

7 where object_id = state_pkg.g_data(i);<br />

8 end loop;<br />

9 end;<br />

10 /<br />

PL/SQL procedure successfully completed.<br />

Next, I ran the preceding block of code three times (<strong>and</strong> the copy of that block of code<br />

where HASHED is replaced with HEAP as well). The first run was to “warm up” the system, to get<br />

any hard parses out of the way. The second time I ran the blocks of code, I used runstats to<br />

see the material differences between the two: running first the hashed implementation <strong>and</strong><br />

then the heap. The third time I ran the blocks of code, I did so with SQL_TRACE enabled so I<br />

could see a TKPROF report. The runstats run reported the following:

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

Saved successfully!

Ooh no, something went wrong!