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.

440<br />

CHAPTER 11 ■ INDEXES<br />

6 end loop;<br />

7 end;<br />

8 /<br />

PL/SQL procedure successfully completed.<br />

ops$tkyte@ORA10G> alter table colocated<br />

2 add constraint colocated_pk<br />

3 primary key(x);<br />

Table altered.<br />

ops$tkyte@ORA10G> begin<br />

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

3 end;<br />

4 /<br />

PL/SQL procedure successfully completed.<br />

This table fits the description we laid out earlier with about 100 rows/block in an 8KB<br />

database. In this table, there is a very good chance that the rows with X=1, 2, 3 are on the same<br />

block. Now, we’ll take this table <strong>and</strong> purposely “disorganize” it. In the COLOCATED table, we created<br />

the Y column with a leading r<strong>and</strong>om number, <strong>and</strong> we’ll use that fact to “disorganize” the<br />

data so that it will definitely not be ordered by primary key anymore:<br />

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

2 as<br />

3 select x,y<br />

4 from colocated<br />

5 order by y;<br />

Table created.<br />

ops$tkyte@ORA10G> alter table disorganized<br />

2 add constraint disorganized_pk<br />

3 primary key (x);<br />

Table altered.<br />

ops$tkyte@ORA10G> begin<br />

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

3 end;<br />

4 /<br />

PL/SQL procedure successfully completed.<br />

Arguably, these are the same tables—it is a relational database, so physical organization<br />

has no bearing on the answers returned (at least that’s what they teach in theoretical database<br />

courses). In fact, the performance characteristics of these two tables are as different as night<br />

<strong>and</strong> day, while the answers returned are identical. Given the same exact question, using the<br />

same exact query plans, <strong>and</strong> reviewing the TKPROF (SQL trace) output, we see the following:

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

Saved successfully!

Ooh no, something went wrong!