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.

CHAPTER 10 ■ DATABASE TABLES 357<br />

7 zip number,<br />

8 primary key (empno,addr_type)<br />

9 )<br />

10 ORGANIZATION INDEX<br />

11 /<br />

Table created.<br />

I populated these tables by inserting into them a work address for each employee, then a<br />

home address, then a previous address, <strong>and</strong> finally a school address. A heap table would tend<br />

to place the data at “the end” of the table; as the data arrives, the heap table would simply add<br />

it to the end, due to the fact that the data is just arriving <strong>and</strong> no data is being deleted. Over<br />

time, if addresses are deleted the inserts would become more r<strong>and</strong>om throughout the table.<br />

But suffice it to say that the odds an employee’s work address would be on the same block as<br />

his home address in the heap table is near zero. For the IOT, however, since the key is on<br />

EMPNO,ADDR_TYPE, we’ll be pretty sure that all of the addresses for a given EMPNO are located<br />

on one or maybe two index blocks together. The inserts used to populate this data were<br />

ops$tkyte@ORA10GR1> insert into heap_addresses<br />

2 select empno, 'WORK', '123 main street', 'Washington', 'DC', 20123<br />

3 from emp;<br />

48250 rows created.<br />

ops$tkyte@ORA10GR1> insert into iot_addresses<br />

2 select empno, 'WORK', '123 main street', 'Washington', 'DC', 20123<br />

3 from emp;<br />

48250 rows created.<br />

I did that three more times, changing WORK to HOME, PREV, <strong>and</strong> SCHOOL in turn. Then I<br />

gathered statistics:<br />

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

PL/SQL procedure successfully completed.<br />

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

PL/SQL procedure successfully completed.<br />

Now we are ready to see what measurable difference we could expect to see. Using<br />

AUTOTRACE, we’ll get a feeling for the change:<br />

ops$tkyte@ORA10GR1> set autotrace traceonly<br />

ops$tkyte@ORA10GR1> select *<br />

2 from emp, heap_addresses<br />

3 where emp.empno = heap_addresses.empno<br />

4 <strong>and</strong> emp.empno = 42;<br />

Execution Plan<br />

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

0 SELECT STATEMENT Optimizer=ALL_ROWS (Cost=8 Card=4 Bytes=336)<br />

1 0 NESTED LOOPS (Cost=8 Card=4 Bytes=336)

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

Saved successfully!

Ooh no, something went wrong!