Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Скачиваний:
40
Добавлен:
16.04.2013
Размер:
4.96 Mб
Скачать

When to Use Function-Based Indexes

Example: Function-Based Index for Language-Dependent Sorting

This example demonstrates how a function-based index can be used to sort based on the collation order for a national language. The NLSSORT function returns a sort key for each name, using the collation sequence GERMAN.

CREATE INDEX Nls_index

ON Nls_tab (NLSSORT(Name, 'NLS_SORT = German'));

The SELECT statement selects all of the contents of the table and orders it by NAME. The rows are ordered using the German collation sequence. The Globalization Support parameters are not needed in the SELECT statement, because in a German session, NLS_SORT is set to German and NLS_COMP is set to ANSI.

SELECT * FROM Nls_tab WHERE Name IS NOT NULL

ORDER BY Name;

Restrictions for Function-Based Indexes

Note the following restrictions for function-based indexes:

Only cost-based optimization can use function-based indexes. Remember to call

DBMS_STATS.GATHER_TABLE_STATISTICS or DBMS_STATS.GATHER_ SCHEMA_STATISTICS, for the function-based index to be effective.

Any top-level or package-level PL/SQL functions that are used in the index expression must be declared as DETERMINISTIC. That is, they always return the same result given the same input, like the UPPER function. You must ensure that the subprogram really is deterministic, because Oracle Database does not check that the assertion is true.

The following semantic rules demonstrate how to use the keyword

DETERMINISTIC:

A top level subprogram can be declared as DETERMINISTIC.

A PACKAGE level subprogram can be declared as DETERMINISTIC in the PACKAGE specification but not in the PACKAGE BODY. Errors are raised if

DETERMINISTIC is used inside a PACKAGE BODY.

A private subprogram (declared inside another subprogram or a PACKAGE BODY) cannot be declared as DETERMINISTIC.

A DETERMINISTIC subprogram can call another subprogram whether the called program is declared as DETERMINISTIC or not.

Selecting an Index Strategy 4-11

When to Use Function-Based Indexes

Expressions used in a function-based index cannot contain any aggregate functions. The expressions should reference only columns in a row in the table.

You must analyze the table or index before the index is used.

Bitmap optimizations cannot use descending indexes.

Function-based indexes are not used when OR-expansion is done.

The index function cannot be marked NOT NULL. To avoid a full table scan, you must ensure that the query cannot fetch null values.

Function-based indexes cannot use expressions that return VARCHAR2 or RAW data types of unknown length from PL/SQL functions. A workaround is to limit the size of the function's output by indexing a substring of known length:

--INITIALS() might return 1 letter, 2 letters, 3 letters, and so on.

--We limit the return value to 10 characters for purposes of the index. CREATE INDEX func_substr_index ON

emp_tab(substr(initials(ename),1,10);

--Call SUBSTR both when creating the index and when referencing

--the function in queries.

SELECT SUBSTR(initials(ename),1,10) FROM emp_tab;

4-12 Oracle Database Application Developer's Guide - Fundamentals

Соседние файлы в папке Oracle 10g