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 11 ■ INDEXES 453<br />

column will need to get exclusive access to two of these index key entries: the index key entry<br />

for the old value <strong>and</strong> the index key entry for the new value. The hundreds of rows these two<br />

entries point to will be unavailable for modification by other sessions until that UPDATE commits.<br />

Bitmap Join Indexes<br />

<strong>Oracle</strong>9i introduced a new index type: the bitmap join index. Normally an index is created on<br />

a single table, using only columns from that table. A bitmap join index breaks that rule <strong>and</strong><br />

allows you to index a given table using columns from some other table. In effect, this allows<br />

you to denormalize data in an index structure instead of in the tables themselves.<br />

Consider the simple EMP <strong>and</strong> DEPT tables. EMP has a foreign key to DEPT (the DEPTNO column).<br />

The DEPT table has the DNAME attribute (the name of the department). The end users will frequently<br />

ask questions such as “How many people work in sales?”, “Who works in sales?”, “Can<br />

you show me the top N performing people in sales?” Note that they do not ask, “How many<br />

people work in DEPTNO 30?” They don’t use those key values; rather, they use the humanreadable<br />

department name. Therefore, they end up running queries such as the following:<br />

select count(*)<br />

from emp, dept<br />

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

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

/<br />

select emp.*<br />

from emp, dept<br />

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

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

/<br />

Those queries almost necessarily have to access the DEPT table <strong>and</strong> the EMP table using<br />

conventional indexes. We might use an index on DEPT.DNAME to find the SALES row(s) <strong>and</strong><br />

retrieve the DEPTNO value for SALES, <strong>and</strong> then using an INDEX on EMP.DEPTNO find the matching<br />

rows, but by using a bitmap join index we can avoid all of that. The bitmap join index allows us<br />

to index the DEPT.DNAME column, but have that index point not at the DEPT table, but at the EMP<br />

table. This is a pretty radical concept—to be able to index attributes from other tables—<strong>and</strong> it<br />

might change the way to implement your data model in a reporting system. You can, in effect,<br />

have your cake <strong>and</strong> eat it, too. You can keep your normalized data structures intact, yet get the<br />

benefits of denormalization at the same time.<br />

Here’s the index we would create for this example:<br />

ops$tkyte@ORA10G> create bitmap index emp_bm_idx<br />

2 on emp( d.dname )<br />

3 from emp e, dept d<br />

4 where e.deptno = d.deptno<br />

5 /<br />

Index created.<br />

Note how the beginning of the CREATE INDEX looks “normal” <strong>and</strong> creates the index<br />

INDEX_NAME on the table. But from there on, it deviates from “normal.” We see a reference to a

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

Saved successfully!

Ooh no, something went wrong!