Select the Appropriate Type of Index
Performance impact: Variable
The following details when it is appropriate to use each type of index.
Hash indexes are useful for finding rows with an exact match on one or more columns. Hash indexes are useful for doing equality searches. A hash index is created with either of the following:
-
You can create a hash index or a unique hash index with the
CREATE [UNIQUE] HASH INDEXstatement. -
You can create a unique hash index when creating your table with the
CREATE TABLE... UNIQUE HASH ONstatement. The unique hash index is specified over the primary key columns of the table.Note:
If you are planning to load your tables with data, consider creating your tables without indexes. After the data is loaded, you can then create your indexes. This reduces the time it takes to load the data into the tables. The exception is if you are using foreign keys and reference tables.
Range indexes are created by default with the CREATE TABLE statement or created with the CREATE [UNIQUE] HASH INDEX statement. Range indexes can speed up exact key lookups but are more flexible and can speed up other queries as well. Select a range index if your queries include LESS THAN or GREATER THAN comparisons. Range indexes are effective for high-cardinality data: that is, data with many possible values, such as CUSTOMER_NAME or PHONE_NUMBER. Range indexes are optimized for in-memory data management.
Range indexes can also be used to speed up "prefix" queries. A prefix query has equality conditions on all but the last key column that is specified. The last column of a prefix query can have either an equality condition or an inequality condition.
Consider the following table and index definitions:
Command> CREATE TABLE T(i1 tt_integer, i2 tt_integer, i3 tt_integer, ...); Command> CREATE INDEX IXT on T(i1, i2, i3);
The index IXT can be used to speed up the following queries:
Command> SELECT * FROM T WHERE i1>12; Command> SELECT * FROM T WHERE i1=12 and i2=75; Command> SELECT * FROM T WHERE i1=12 and i2 BETWEEN 10 and 20; Command> SELECT * FROM T WHERE i1=12 and i2=75 and i3>30;
The index IXT is not used for the following queries, because the prefix property is not satisfied:
Command> SELECT * FROM T WHERE i2=12;
There is no equality condition for i1.
The index IXT is used, but matching only occurs on the first two columns for queries like the following:
Command> SELECT * FROM T WHERE i1=12 and i2<50 and i3=630;
Range indexes have a dynamic structure that adjusts itself automatically to accommodate changes in table size. A range index can be either unique or non-unique and can be declared over nullable columns. It also allows the indexed column values to be changed once a record is inserted. A range index is likely to be more compact than an equivalent hash index.