Function Based Indexes
Traditionally, performing a function on an indexed column in the where clause of a query guaranteed an index would not be used. Oracle 8i introduced Function Based Indexes to counter this problem. Rather than indexing a column, you index the function on that column, storing the product of the function, not the original column data. When a query is passed to the server that could benefit from that index, the query is rewritten to allow the index to be used. The following code samples give an example of the use of Function Based Indexes:
* Build Test Table
* Build Regular Index
* Build Function Based Index
Build Test Table
First we build a test table and populate it with enough data so that use of an index would be advantageous:
Code: |
CREATE TABLE user_data ( id NUMBER(10) NOT NULL, first_name VARCHAR2(40) NOT NULL, last_name VARCHAR2(40) NOT NULL); BEGIN ANALYZE TABLE user_data COMPUTE STATISTICS; At this point the table is not indexed so we would expect a full table scan for any query: SET AUTOTRACE ON Execution Plan Build Regular Index CREATE INDEX first_name_idx ON user_data (first_name); SET AUTOTRACE ON Execution Plan Build Function Based Index DROP INDEX first_name_idx; ALTER SESSION SET QUERY_REWRITE_INTEGRITY = TRUSTED; SET AUTOTRACE ON [b] 2 1 INDEX (RANGE SCAN) OF ‘FIRST_NAME_IDX’ (NON-UNIQUE) (Cost=1 Card=1)[/b] |
The QUERY_REWRITE_INTEGRITY and QUERY_REWRITE_ENABLED parameters must be set or the server will not be able to rewrite the queries, and will therefore not be able to use the new index. These parameters may be set in the Init.ora file if they are needed permanently.
Leave a Reply
You must be logged in to post a comment.