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

rekharaghuram
from rekharaghuram More from this publisher
05.11.2015 Views

CHAPTER 10 ■ DATABASE TABLES 357 7 zip number, 8 primary key (empno,addr_type) 9 ) 10 ORGANIZATION INDEX 11 / Table created. I populated these tables by inserting into them a work address for each employee, then a home address, then a previous address, and finally a school address. A heap table would tend to place the data at “the end” of the table; as the data arrives, the heap table would simply add it to the end, due to the fact that the data is just arriving and no data is being deleted. Over time, if addresses are deleted the inserts would become more random throughout the table. But suffice it to say that the odds an employee’s work address would be on the same block as his home address in the heap table is near zero. For the IOT, however, since the key is on EMPNO,ADDR_TYPE, we’ll be pretty sure that all of the addresses for a given EMPNO are located on one or maybe two index blocks together. The inserts used to populate this data were ops$tkyte@ORA10GR1> insert into heap_addresses 2 select empno, 'WORK', '123 main street', 'Washington', 'DC', 20123 3 from emp; 48250 rows created. ops$tkyte@ORA10GR1> insert into iot_addresses 2 select empno, 'WORK', '123 main street', 'Washington', 'DC', 20123 3 from emp; 48250 rows created. I did that three more times, changing WORK to HOME, PREV, and SCHOOL in turn. Then I gathered statistics: ops$tkyte@ORA10GR1> exec dbms_stats.gather_table_stats( user, 'HEAP_ADDRESSES' ); PL/SQL procedure successfully completed. ops$tkyte@ORA10GR1> exec dbms_stats.gather_table_stats( user, 'IOT_ADDRESSES' ); PL/SQL procedure successfully completed. Now we are ready to see what measurable difference we could expect to see. Using AUTOTRACE, we’ll get a feeling for the change: ops$tkyte@ORA10GR1> set autotrace traceonly ops$tkyte@ORA10GR1> select * 2 from emp, heap_addresses 3 where emp.empno = heap_addresses.empno 4 and emp.empno = 42; Execution Plan ---------------------------------------------------------- 0 SELECT STATEMENT Optimizer=ALL_ROWS (Cost=8 Card=4 Bytes=336) 1 0 NESTED LOOPS (Cost=8 Card=4 Bytes=336)

358 CHAPTER 10 ■ DATABASE TABLES 2 1 TABLE ACCESS (BY INDEX ROWID) OF 'EMP' (TABLE) (Cost=2 Card=1... 3 2 INDEX (UNIQUE SCAN) OF 'EMP_PK' (INDEX (UNIQUE)) (Cost=1 Card=1) 4 1 TABLE ACCESS (BY INDEX ROWID) OF 'HEAP_ADDRESSES' (TABLE) (Cost=6... 5 4 INDEX (RANGE SCAN) OF 'SYS_C008078' (INDEX (UNIQUE)) (Cost=2 Card=4) Statistics ---------------------------------------------------------- ... 11 consistent gets ... 4 rows processed That is a pretty common plan: go to the EMP table by primary key; get the row; then using that EMPNO, go to the address table; and using the index, pick up the child records. We did 11 I/Os to retrieve this data. Now running the same query, but using the IOT for the addresses ops$tkyte@ORA10GR1> select * 2 from emp, iot_addresses 3 where emp.empno = iot_addresses.empno 4 and emp.empno = 42; Execution Plan ---------------------------------------------------------- 0 SELECT STATEMENT Optimizer=ALL_ROWS (Cost=4 Card=4 Bytes=336) 1 0 NESTED LOOPS (Cost=4 Card=4 Bytes=336) 2 1 TABLE ACCESS (BY INDEX ROWID) OF 'EMP' (TABLE) (Cost=2 Card=1... 3 2 INDEX (UNIQUE SCAN) OF 'EMP_PK' (INDEX (UNIQUE)) (Cost=1 Card=1) 4 1 INDEX (RANGE SCAN) OF 'SYS_IOT_TOP_59615' (INDEX (UNIQUE)) (Cost=2... Statistics ---------------------------------------------------------- ... 7 consistent gets ... 4 rows processed ops$tkyte@ORA10GR1> set autotrace off we did four fewer I/Os (the four should have been guessable); we skipped four TABLE ACCESS ➥ (BY INDEX ROWID) steps. The more child records we have, the more I/Os we would anticipate skipping. So, what is four I/Os? Well, in this case it was over one-third of the I/O performed for the query, and if we execute this query repeatedly, that would add up. Each I/O and each consistent get requires an access to the buffer cache, and while it is true that reading data out of the buffer cache is faster than disk, it is also true that the buffer cache gets are not free and not totally cheap. Each will require many latches of the buffer cache, and latches are serialization devices that will inhibit our ability to scale. We can measure both the I/O reduction as well as latching reduction by running a PL/SQL block such as this:

358<br />

CHAPTER 10 ■ DATABASE TABLES<br />

2 1 TABLE ACCESS (BY INDEX ROWID) OF 'EMP' (TABLE) (Cost=2 Card=1...<br />

3 2 INDEX (UNIQUE SCAN) OF 'EMP_PK' (INDEX (UNIQUE)) (Cost=1 Card=1)<br />

4 1 TABLE ACCESS (BY INDEX ROWID) OF 'HEAP_ADDRESSES' (TABLE) (Cost=6...<br />

5 4 INDEX (RANGE SCAN) OF 'SYS_C008078' (INDEX (UNIQUE)) (Cost=2 Card=4)<br />

Statistics<br />

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

...<br />

11 consistent gets<br />

...<br />

4 rows processed<br />

That is a pretty common plan: go to the EMP table by primary key; get the row; then using<br />

that EMPNO, go to the address table; <strong>and</strong> using the index, pick up the child records. We did 11<br />

I/Os to retrieve this data. Now running the same query, but using the IOT for the addresses<br />

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

2 from emp, iot_addresses<br />

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

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

Execution Plan<br />

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

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

1 0 NESTED LOOPS (Cost=4 Card=4 Bytes=336)<br />

2 1 TABLE ACCESS (BY INDEX ROWID) OF 'EMP' (TABLE) (Cost=2 Card=1...<br />

3 2 INDEX (UNIQUE SCAN) OF 'EMP_PK' (INDEX (UNIQUE)) (Cost=1 Card=1)<br />

4 1 INDEX (RANGE SCAN) OF 'SYS_IOT_TOP_59615' (INDEX (UNIQUE)) (Cost=2...<br />

Statistics<br />

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

...<br />

7 consistent gets<br />

...<br />

4 rows processed<br />

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

we did four fewer I/Os (the four should have been guessable); we skipped four TABLE ACCESS ➥<br />

(BY INDEX ROWID) steps. The more child records we have, the more I/Os we would anticipate<br />

skipping.<br />

So, what is four I/Os? Well, in this case it was over one-third of the I/O performed for the<br />

query, <strong>and</strong> if we execute this query repeatedly, that would add up. Each I/O <strong>and</strong> each consistent<br />

get requires an access to the buffer cache, <strong>and</strong> while it is true that reading data out of the<br />

buffer cache is faster than disk, it is also true that the buffer cache gets are not free <strong>and</strong> not<br />

totally cheap. Each will require many latches of the buffer cache, <strong>and</strong> latches are serialization<br />

devices that will inhibit our ability to scale. We can measure both the I/O reduction as well as<br />

latching reduction by running a PL/SQL block such as this:

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

Saved successfully!

Ooh no, something went wrong!