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.

454<br />

CHAPTER 11 ■ INDEXES<br />

column in the DEPT table: D.DNAME. We see a FROM clause, making this CREATE INDEX statement<br />

resemble a query. We have a join condition between multiple tables. This CREATE INDEX statement<br />

indexes the DEPT.DNAME column, but in the context of the EMP table. If we ask those<br />

questions mentioned earlier, we would find the database never accesses the DEPT at all, <strong>and</strong> it<br />

need not do so because the DNAME column now exists in the index pointing to rows in the EMP<br />

table. For purposes of illustration, we will make the EMP <strong>and</strong> DEPT tables appear “large” (to<br />

avoid having the CBO think they are small <strong>and</strong> full scanning them instead of using indexes):<br />

ops$tkyte@ORA10G> begin<br />

2 dbms_stats.set_table_stats( user, 'EMP',<br />

3 numrows => 1000000, numblks => 300000 );<br />

4 dbms_stats.set_table_stats( user, 'DEPT',<br />

5 numrows => 100000, numblks => 30000 );<br />

6 end;<br />

7 /<br />

PL/SQL procedure successfully completed.<br />

<strong>and</strong> then we’ll perform our queries:<br />

ops$tkyte@ORA10G> set autotrace traceonly explain<br />

ops$tkyte@ORA10G> select count(*)<br />

2 from emp, dept<br />

3 where emp.deptno = dept.deptno<br />

4 <strong>and</strong> dept.dname = 'SALES'<br />

5 /<br />

Execution Plan<br />

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

0 SELECT STATEMENT Optimizer=ALL_ROWS (Cost=1 Card=1 Bytes=13)<br />

1 0 SORT (AGGREGATE)<br />

2 1 BITMAP CONVERSION (COUNT) (Cost=1 Card=10000 Bytes=130000)<br />

3 2 BITMAP INDEX (SINGLE VALUE) OF 'EMP_BM_IDX' (INDEX (BITMAP))<br />

As you can see, to answer this particular question, we did not have to actually access<br />

either the EMP or DEPT table—the entire answer came from the index itself. All the information<br />

needed to answer the question was available in the index structure.<br />

Further, we were able to skip accessing the DEPT table <strong>and</strong>, using the index on EMP that<br />

incorporated the data we needed from DEPT, gain direct access to the required rows:<br />

ops$tkyte@ORA10G> select emp.*<br />

2 from emp, dept<br />

3 where emp.deptno = dept.deptno<br />

4 <strong>and</strong> dept.dname = 'SALES'<br />

5 /<br />

Execution Plan<br />

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

0 SELECT STATEMENT Optimizer=ALL_ROWS (Cost=6145 Card=10000 Bytes=870000)<br />

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

2 1 BITMAP CONVERSION (TO ROWIDS)<br />

3 2 BITMAP INDEX (SINGLE VALUE) OF 'EMP_BM_IDX' (INDEX (BITMAP))

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

Saved successfully!

Ooh no, something went wrong!