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 361<br />

ops$tkyte@ORA10GR1> create table stocks<br />

2 ( ticker varchar2(10),<br />

3 day date,<br />

4 value number,<br />

5 change number,<br />

6 high number,<br />

7 low number,<br />

8 vol number,<br />

9 primary key(ticker,day)<br />

10 )<br />

11 organization index<br />

12 /<br />

Table created.<br />

I frequently look at one stock at a time for some range of days (e.g., computing a moving<br />

average). If I were to use a heap organized table, the probability of two rows for the stock ticker<br />

ORCL existing on the same database block are almost zero. This is because every night, I insert<br />

the records for the day for all of the stocks. That fills up at least one database block (actually,<br />

many of them). Therefore, every day I add a new ORCL record, but it is on a block different from<br />

every other ORCL record already in the table. If I query as follows:<br />

Select * from stocks<br />

where ticker = 'ORCL'<br />

<strong>and</strong> day between sysdate-100 <strong>and</strong> sysdate;<br />

<strong>Oracle</strong> would read the index <strong>and</strong> then perform table access by rowid to get the rest of the row<br />

data. Each of the 100 rows I retrieve would be on a different database block due to the way I<br />

load the table—each would probably be a physical I/O. Now consider that I have this same<br />

data in an IOT. That same query only needs to read the relevant index blocks, <strong>and</strong> it already<br />

has all of the data. Not only is the table access removed, but all of the rows for ORCL in a given<br />

range of dates are physically stored “near” each other as well. Less logical I/O <strong>and</strong> less physical<br />

I/O is incurred.<br />

Now you underst<strong>and</strong> when you might want to use IOTs <strong>and</strong> how to use them. What you<br />

need to underst<strong>and</strong> next is what the options are with these tables. What are the caveats?<br />

The options are very similar to the options for a heap organized table. Once again, we’ll use<br />

DBMS_METADATA to show us the details. Let’s start with the three basic variations of the IOT:<br />

ops$tkyte@ORA10GR1> create table t1<br />

2 ( x int primary key,<br />

3 y varchar2(25),<br />

4 z date<br />

5 )<br />

6 organization index;<br />

Table created.<br />

ops$tkyte@ORA10GR1> create table t2<br />

2 ( x int primary key,<br />

3 y varchar2(25),<br />

4 z date

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

Saved successfully!

Ooh no, something went wrong!