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.

452<br />

CHAPTER 11 ■ INDEXES<br />

This example shows the power of the bitmap indexes. <strong>Oracle</strong> is able to see the location<br />

in (1,10,30) <strong>and</strong> knows to read the index on location for these three values <strong>and</strong> logically OR<br />

together the “bits” in the bitmap. It then takes that resulting bitmap <strong>and</strong> logically ANDs that<br />

with the bitmaps for AGE_GROUP='41 AND OVER' <strong>and</strong> GENDER='M'. Then a simple count of 1s<br />

<strong>and</strong> the answer is ready.<br />

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

2 from t<br />

3 where ( ( gender = 'M' <strong>and</strong> location = 20 )<br />

4 or ( gender = 'F' <strong>and</strong> location = 22 ))<br />

5 <strong>and</strong> age_group = '18 <strong>and</strong> under';<br />

Execution Plan<br />

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

0 SELECT STATEMENT Optimizer=ALL_ROWS (Cost=77 Card=507 Bytes=16731)<br />

1 0 TABLE ACCESS (BY INDEX ROWID) OF 'T' (TABLE) (Cost=77 Card=507 ...<br />

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

3 2 BITMAP AND<br />

4 3 BITMAP INDEX (SINGLE VALUE) OF 'AGE_GROUP_IDX' (INDEX (BITMAP))<br />

5 3 BITMAP OR<br />

6 5 BITMAP AND<br />

7 6 BITMAP INDEX (SINGLE VALUE) OF 'LOCATION_IDX' (INDEX (BITMAP))<br />

8 6 BITMAP INDEX (SINGLE VALUE) OF 'GENDER_IDX' (INDEX (BITMAP))<br />

9 5 BITMAP AND<br />

10 9 BITMAP INDEX (SINGLE VALUE) OF 'GENDER_IDX' (INDEX (BITMAP))<br />

11 9 BITMAP INDEX (SINGLE VALUE) OF 'LOCATION_IDX' (INDEX (BITMAP))<br />

This shows similar logic: the plan shows the OR’d conditions are each evaluated by AND-ing<br />

together the appropriate bitmaps <strong>and</strong> then OR-ing together those results. Throw in another AND<br />

to satisfy the AGE_GROUP='18 AND UNDER' <strong>and</strong> we have it all. Since we asked for the actual rows<br />

this time, <strong>Oracle</strong> will convert each bitmap 1 <strong>and</strong> 0 into rowids to retrieve the source data.<br />

In a data warehouse or a large reporting system supporting many ad hoc SQL queries, this<br />

ability to use as many indexes as make sense simultaneously comes in very h<strong>and</strong>y indeed.<br />

Using conventional B*Tree indexes here would not be nearly as usual or usable, <strong>and</strong> as the<br />

number of columns that are to be searched by the ad hoc queries increases, the number of<br />

combinations of B*Tree indexes you would need increases as well.<br />

However, there are times when bitmaps are not appropriate. They work well in a readintensive<br />

environment, but they are extremely ill suited for a write-intensive environment.<br />

The reason is that a single bitmap index key entry points to many rows. If a session modifies<br />

the indexed data, then all of the rows that index entry points to are effectively locked in most<br />

cases. <strong>Oracle</strong> cannot lock an individual bit in a bitmap index entry; it locks the entire bitmap<br />

index entry. Any other modifications that need to update that same bitmap index entry will be<br />

locked out. This will seriously inhibit concurrency, as each update will appear to lock potentially<br />

hundreds of rows preventing their bitmap columns from being concurrently updated. It<br />

will not lock every row as you might think—just many of them. Bitmaps are stored in chunks,<br />

so using the earlier EMP example we might find that the index key ANALYST appears in the index<br />

many times, each time pointing to hundreds of rows. An update to a row that modifies the JOB

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

Saved successfully!

Ooh no, something went wrong!