5.2.3 Examples of indexes that are used during retrieval of a table (examples of index priority)
If multiple indexes are defined for a table, the index to be used is determined on the basis of the priority shown in Table 5‒1: Index priority when multiple indexes are defined for a table.
This section explains typical examples of the priority of indexes that are used during retrieval of a table.
If you want to check the index that will actually be used for a retrieval, see 5.2.5 How to check the index used during execution of an SQL statement.
- Organization of this subsection
(1) Example 1 (priority between single-column indexes)
The following is an example of the relative priority between B-tree indexes (single-column indexes):
Example of the B-tree index definition:
CREATE INDEX "IDX_C1" ON "T1" ("C1") IN "DBAREA01" EMPTY CREATE INDEX "IDX_C2" ON "T1" ("C2") IN "DBAREA01" EMPTY
Example of the SELECT statement to be executed:
SELECT "C1","C2","C3" FROM "T1" WHERE "C1">100 AND "C2"='U0100'
In this example, the B-tree index IDX_C1 has a priority of 15, and the B-tree index IDX_C2 has a priority of 2. As such, the B-tree index IDX_C2 is used for retrieval.
- Important
-
In the example above, if only one of the B-tree indexes is selected to be used on the basis of the priority, change the search condition, if possible, in such a manner that the B-tree index that can narrow the search most effectively will be used. You can expect an improvement in performance by using a B-tree index that provides more effective narrowing.
(2) Example 2 (priority between single-column indexes)
The following is an example of the relative priority between B-tree indexes (single-column indexes):
Example of the B-tree index definition:
CREATE INDEX "IDX_C1" ON "T1" ("C1") IN "DBAREA01" EMPTY CREATE INDEX "IDX_C2" ON "T1" ("C2") IN "DBAREA01" EMPTY
Example of the SELECT statement to be executed:
SELECT * FROM "T1" WHERE "C2"='U0100' AND "C1"=100
In this example, B-tree indexes IDX_C1 and IDX_C2 both have a priority of 2. Because a B-tree index for the first column specified in the search condition has the higher priority, B-tree index IDX_C2 is used for retrieval.
(3) Example 3 (priority between multiple-column indexes)
The following is an example of the relative priority between B-tree indexes (multiple-column indexes):
Example of the B-tree index definition:
CREATE INDEX "IDX_C1C2" ON "T1" ("C1","C2") IN "DBAREA01" EMPTY CREATE INDEX "IDX_C2C3" ON "T1" ("C2","C3") IN "DBAREA01" EMPTY
Example of the SELECT statement to be executed:
SELECT "C1","C2","C3" FROM "T1" WHERE "C2"='U0100'
In this example, B-tree index IDX_C2C3 is used.
IDX_C1C2 has column C2 as its second indexed column, while IDX_C2C3 has column C2 as its first indexed column, so IDX_C2C3 is used.
(4) Example 4 (priority between single-column index and multiple-column index)
The following is an example of the relative priority between single-column and multiple-column B-tree indexes:
Example of the B-tree index definition:
CREATE INDEX "IDX_C1" ON "T1" ("C1") IN "DBAREA01" EMPTY CREATE INDEX "IDX_C3C2" ON "T1" ("C3","C2") IN "DBAREA01" EMPTY
Example of the SELECT statement to be executed:
SELECT "C1","C2","C3" FROM "T1" WHERE "C1">100 AND "C2"='U0100'
In this example, B-tree index IDX_C1 is used.
IDX_C3C2 has column C2 as its second indexed column, while IDX_C1 has column C1 as its indexed column, so IDX_C1 is used.
(5) Example 5 (priority between single-column index and multiple-column index)
The following is an example of the relative priority between single-column and multiple-column B-tree indexes:
Example of the B-tree index definition:
CREATE INDEX "IDX_C1" ON "T1" ("C1") IN "DBAREA01" EMPTY CREATE INDEX "IDX_C2C3" ON "T1" ("C2","C3") IN "DBAREA01" EMPTY
Example of the SELECT statement to be executed:
SELECT "C1","C2","C3" FROM "T1" WHERE "C1">100 AND "C2"='U0100'
In this example, the B-tree index IDX_C1 has a priority of 15, and the B-tree index IDX_C2C3 has a priority of 2. As such, the B-tree index IDX_C2C3 is used.
IDX_C1 that has column C1 as an indexed column and IDX_C2C3 that has column C2 as the first indexed column are subject to comparison to determine priority, as shown in Table 5‒1: Index priority when multiple indexes are defined for a table.
(6) Example 6 (priority between text index and B-tree index)
The following is an example of the relative priority of a text index and B-tree index (single-column index).
Example of index definitions:
CREATE INDEX "IDX_TXT_C1" <- Definition of text index ON "T1" ("C1") IN "DBAREA01" EMPTY INDEXTYPE TEXT CREATE INDEX "IDX_C2" <- Definition of B-tree index ON "T1" ("C2") IN "DBAREA01" EMPTY
Example of SELECT statement to execute:
SELECT "C1","C2","C3" FROM "T1" WHERE "C1" LIKE '%XYZ%' AND "C2" LIKE 'ABC%'
In this example, the text index IDX_TXT_C1 has a priority of 8, and the B-tree index IDX_C2 has a priority of 4. As such, the B-tree index IDX_C2 is used for retrieval.
- Important
-
In this scenario, if the relative priority results in only one of the indexes being used, if possible, change the search condition so that the index that can narrow the search scope most effectively is used. You can expect to see an improvement in performance by using an index that more efficiently narrows the search scope.
(7) If you want to change the index to be used during retrieval
This subsection explains how to change the index to be used during retrieval.
(a) If there are multiple indexed columns with the same priority value, specify the one that you want to use first
As explained in (2) Example 2 (priority between single-column indexes), a B-tree index for the first column specified in the search condition is used if multiple indexes with the same priority value are specified. You can use this rule to change the B-tree index to be used. The following example changes the search condition specification:
- Before change:
-
SELECT * FROM "T1" WHERE "C3"='A001' AND "C2"='U0100'
- After change:
-
SELECT * FROM "T1" WHERE "C2"='U0100' AND "C3"='A001'
Before the change, B-tree index IDX_C3 defined for column C3 would be used, but after change, B-tree index IDX_C2 defined for column C2 is used.
(b) To lower the priority value of the current index that is used
You can lower the priority value of the index that would be used, so that another index will be used. The following example changes the search condition specification:
- Before change:
-
SELECT * FROM "T1" WHERE "C1"=100 AND "C2" LIKE 'ABC%'
- After change:
-
SELECT * FROM "T1" WHERE "C1" BETWEEN 100 AND 100 AND "C2" LIKE 'ABC%'
Before the change, the priority of IDX_C1 was 2 and the priority of IDX_C2 was 4, but after the change, the priority of IDX_C1 is 13. Therefore, B-tree index IDX_C1 would have been used before the change, but B-tree index IDX_C2 will be used after the change.
(c) Using the index specification
You can specify the index to be used for retrieval. The following shows an example.
- Before change:
-
SELECT * FROM "T1" WHERE "C1"=100 AND "C2" LIKE 'ABC%'
The priority of B-tree index IDX_C1 defined for column C1 is 2 and the priority of B-tree index IDX_C2 defined for column C2 is 4. Therefore, B-tree index IDX_C1 is used.
- After change:
-
SELECT * FROM "T1" /*>> WITH INDEX (IDX_C2) <<*/ WHERE "C1"=100 AND "C2" LIKE 'ABC%'
You can specify the index to be used for retrieval by using the index specification indicated by the underlining. When the SELECT statement shown here is executed, index IDX_C2 will be used.
For details about the index specification, see Index specification in the manual HADB SQL Reference.