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.

456<br />

CHAPTER 11 ■ INDEXES<br />

■Note The following information applies only to <strong>Oracle</strong>9i Release 1 <strong>and</strong> before. In <strong>Oracle</strong>9i Release 2 <strong>and</strong><br />

above, function-based indexes are usable without any setup. The <strong>Oracle</strong> SQL Reference manual for <strong>Oracle</strong>9i<br />

Release 2 is not correct in this regard—it says you need these privileges, when in fact you do not.<br />

There are some system parameter or session settings you must use <strong>and</strong>, to be able to<br />

create them, a privilege you must have:<br />

• You must have the system privilege QUERY REWRITE to create function-based indexes on<br />

tables in your own schema.<br />

• You must have the system privilege GLOBAL QUERY REWRITE to create function-based<br />

indexes on tables in other schemas.<br />

• For the optimizer to use function-based indexes, the following session or system variables<br />

must be set: QUERY_REWRITE_ENABLED=TRUE <strong>and</strong> QUERY_REWRITE_INTEGRITY=TRUSTED.<br />

You may enable these either at the session level with ALTER SESSION, or at the system<br />

level via ALTER SYSTEM, or by setting them in the init.ora parameter file.<br />

QUERY_REWRITE_ENABLED allows the optimizer to rewrite the query to use the functionbased<br />

index. QUERY_REWRITE_INTEGRITY tells the optimizer to “trust” that the code<br />

marked deterministic by the programmer is in fact deterministic (the following section<br />

contains examples of deterministic code <strong>and</strong> its meaning). If the code is in fact not<br />

deterministic (i.e., it returns different output given the same inputs), the resulting rows<br />

retrieved via the index may be incorrect. You must take care to ensure that a function<br />

defined as deterministic is in fact deterministic.<br />

In all releases, the following points apply:<br />

• Use the cost-based optimizer (CBO). The virtual columns (columns with functions<br />

applied to them) in the function-based indexes are only visible to the CBO <strong>and</strong> will not<br />

be used by the rule-based optimizer (RBO) ever. The RBO can make use of the leadingedge<br />

columns in a function-based index that have no functions applied to them.<br />

• Use SUBSTR to constrain return values from user-written functions that return VARCHAR2<br />

or RAW types. Optionally hide the SUBSTR in a view (recommended). Again, the following<br />

section contains examples of this.<br />

Once the preceding criteria have been satisfied, function-based indexes are as easy to<br />

use as the CREATE INDEX comm<strong>and</strong>. The optimizer will find <strong>and</strong> use your indexes at runtime<br />

for you.<br />

A Simple Function-Based Index Example<br />

Consider the following example. We want to perform a case-insensitive search on the ENAME<br />

column of the EMP table. Prior to function-based indexes, we would have approached this in<br />

a very different manner. We would have added an extra column to the EMP table called<br />

UPPER_ENAME, for example. This column would have been maintained by a database trigger on

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

Saved successfully!

Ooh no, something went wrong!