| Oracle® Database Application Developer's Guide - Fundamentals<
br>
10g Release 1 (10.1) Part Number B10795-01 |
|
|
View PDF |
This chapter discusses considerations for using the differ ent types of indexes in an application. The topics include:
See Also:
|
You can create indexes on columns to speed up queries. Indexes provide faster access to data for ope rations that return a small portion of a table's rows.
In general, you should create an ind ex on a column in any of the following situations:
UNIQUE key integrity constraint exists on the column.You can create an index on any column; however, if the column is not used in any of these si tuations, creating an index on the column does not increase performance and the index takes up resources unnecessarily.
Although the database creates an index for you on a column with an integrity constraint, explicitly crea ting an index on such a column is recommended.
You can use the following techniques to dete rmine which columns are best candidates for indexing:
EXPLAIN PLAN feature to show a theoretical execution plan of a given query statement.V$SQL_PLAN view to determine the actual execution plan used for a given query statement.Sometimes, if an index is not being used by default and it would be most efficient to u se that index, you can use a query hint so that the index is used.
| See Also:
Oracle Database Performance Tuning Guide for information on using the |
The following sections explain how to create, alter, and drop indexes usin g SQL commands, and give guidelines for managing indexes.
Typically, you insert or load data into a table (using SQL*Loader or Import) before creating indexes. Otherwis e, the overhead of updating the index slows down the insert or load operation. The exception to this rule is that you must create an index for a cluster before you insert any data into the cluster.
When you create an index on a table that alread
y has data, Oracle Database must use sort space to create the index. The database uses the sort space in memory allocated for the cre
ator of the index (the amount for each user is determined by the initialization parameter SORT_AREA_SIZE), but the datab
ase must also swap sort information to and from temporary segments allocated on behalf of the index creation. If the index is extreme
ly large, it can be beneficial to complete the following steps:
CREATE TABLESPACE command.TEMPORARY TABLESPACE option of the ALTER USER command to make this y
our new temporary tablespace.CREATE I
NDEX command.DROP TABLESPA
CE command. Then use the ALTER USER command to reset your temporary tablespace to your original temporary tablesp
ace.Under certain conditions, you can load data into a table with the SQL*Loader "di rect path load", and an index can be created as data is loaded.
| See Also:
Oracle Database Utilities for information on direct path load |
See Also:
|
Function-based indexes:
WHERE clause:
CREATE INDEX Idx ON Example_tab(Column_a + Column_b); SELECT * FROM Example_tab WHERE Column_a + Column_b < 10;
The optimizer can use a range scan for this query because the index is built
on (column_a + column_b). Range scans typically produce fast response times if the predicate selects less t
han 15% of the rows of a large table. The optimizer can estimate how many rows are selected by expressions more accurately if the exp
ressions are materialized in a function-based index. (Expressions of function-based indexes are represented as virtual columns and
MAP method to build indexes on an object type column.UPPER and LOWER functions, desc
ending order sorts with the DESC keyword, and linguistic-based sorts with the NLSSORT function.
Another function-based index c
alls the object method distance_from_equator for each city in the table. The method is applied to the object column
CREATE INDEX Distance_index ON Weatherdata_tab (Distance_from_equator (Reg_obj)); < a name="1006432"> SELECT * FROM Weatherdata_tab WHERE (Distance_from_equator (Reg_Ob j)) > '1000';
Another index stores the temperature delta and t he maximum temperature. The result of the delta is sorted in descending order. A query could use this index to quickly find table row s where the temperature delta is less than 20 and the maximum temperature is greater than 75.
< /a>CREATE INDEX compare_index ON Weatherdata_tab ((Maxtemp - Mintemp) DESC, Maxtemp); < a name="1006440">SELECT * FROM Weatherdata_tab WHERE ((Maxtemp - Mintemp) < '20' AND Maxtemp > '75');
The following command allows faster case-insensitive searches in table EMP_TAB.
CREATE INDEX Idx ON Emp_tab (UPPER(Ename));a>
The SELECT command uses the function-based index on UPPER(e_name) to return a
ll of the employees with name like :KEYCOL.
SELECT * FROM Emp_tab WHERE UPPER( Ename) like :KEYCOL;
The following command computes a value for each row using columns A, B, and C, and stores the results in the inde x.
CREATE INDEX Idx ON Fbi_tab (A + B * (C - 1), A, B);
The SELECT statement can either use index range scan (since the expression is a prefix
of index IDX) or index fast full scan (which may be preferable if the index has specified a high parallel degree).
This example demonstrates how a function-based index ca
n 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 u
sing 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;< a name="1006464">
Note the following restrictions for functio n-based indexes:
DBMS_STATS.GATHER_TABLE_STATISTICS or DBMS_STATS.GATHER_SCHEMA_STATISTICS,
for the function-based index to be effective.DETERMINISTIC. That is, they always return th
e 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 sema
ntic rules demonstrate how to use the keyword DETERMINISTIC:
DETERMINISTIC.PACKAGE level subprogram can be declared as DETERMINISTIC in the PACKAGE specification b
ut not in the PACKAGE BODY. Errors are raised if DETERMINISTIC is used inside a PACKAGE<
/code> BODY.PACKAGE BODY) cannot be declared as DETERMINISTIC.DETERMINISTIC subprogram can call another subprogram whether the called program is declared as <
code>DETERMINISTIC or not.NOT NULL. To avoid a full table scan, you must ensure that the query cannot fetch null values.VARCHAR2 or <
code>RAW data types of unknown length from PL/SQL functions. A workaround is to limit the size of the function's output by ind
exing 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. CREA TE 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;