Create a secondary index on index-organized table.
Creating secondary Indexes on Index Organized Tables
Index-organized tables are ideal for situations in which a limited amount of information is associated with an index value.
You can access the values in an index-organized table through the primary key or any valid prefix of the primary key. There may be times when you want to add another index that is not based on the primary key to an index-organized table.
Oracle gives you the ability to add these secondary indexes to index-organized tables.
Syntax for Creating a Secondary Index
The syntax for creating a secondary index on an index-organized table is exactly the same as the syntax for creating a normal index, as
shown in the following diagram:
CREATE INDEX index_name ON column_list;
CREATE INDEX
Required keywords.
index_name
The name of the index you will create
ON
Required keyword
column_list
A list of columns in the index, separated by columns
Creating a Secondary Index on an index-organized Table in Oracle
You can create secondary indexes on an index-organized tables to provide multiple access paths. Secondary indexes on index-organized tables differ from indexes on ordinary tables in two ways:
They store logical rowids instead of physical rowids. This is necessary because the inherent movability of rows in a B-tree index results in the rows having no permanent physical addresses. If the physical location of a row changes, its logical rowid remains valid. One effect of this is that a table maintenance operation, such as ALTER TABLE ... MOVE, does not make the secondary index unusable.
The logical rowid also includes a physical guess which identifies the database block address at which the row is likely to be found. If the physical guess is correct, a secondary index scan would incur a single additional I/O once the secondary key is found. The performance would be similar to that of a secondary index-scan on an ordinary table.
Unique and non-unique secondary indexes, function-based secondary indexes, and bitmap indexes are supported as secondary indexes on index-organized tables.
Creating a Secondary Index on an Index-Organized Table
The following statement shows the creation of a secondary index on the docindex index-organized table where doc_id and token are the key columns:
CREATE INDEX Doc_id_index on Docindex(Doc_id, Token);
This secondary index allows the database to efficiently process a query, such as the following, the involves a predicate on doc_id:
SELECT Token FROM Docindex
WHERE Doc_id = 1;
Secondary index Functionality
Secondary indexes on an index-organized table are createdin the same manner as regular indexes on regular tables, but function somewhat differently. Remember how a normal index works: The index structure contains the index value, which then points to the associated row in the database table. The index entry points to the associated table through the use of a ROWID. An index-organized table does not have any associated table rows, and therefore does not have any ROWIDs. Because of this, a secondary index on an index-organized table uses a logical ROWID. A logical ROWID is a ROWID that is based on a physical guess as to which block in the leaf node (of the index) contains a particular value. If the primary key of the value does not change, the logical ROWID remains accurate. If the primary key value ever changes, the logical ROWID becomes invalid. The logical ROWIDs for an index-organized table are reassigned whenever the index is rebuilt.
Index Organized Table Enhancements
There were a number of enhancements to index organized tables as of Oracle9i, including a lifting of the restriction against the use of bitmap indexes as secondary indexes for an IOT and the ability to create, rebuild, or coalesce secondary indexes on an indexed organized table. Oracle Database 10g continued this trend by allowing replication and all types of partitioning for index organized tables.
Creating a Bitmap Index on an Index-Organized Table
Bitmap indexes can be created on index-organized tables (IOT) as secondary indexes on the IOT, but they must be created with a mapping table. See the following example of creating the IOT:
CREATE TABLE employees_part
(
EMPLOYEE_ID NUMBER(6) NOT NULL
,FIRST_NAME VARCHAR2(20)
,LAST_NAME VARCHAR2(25) NOT NULL
,EMAIL VARCHAR2(25) NOT NULL
,PHONE_NUMBER VARCHAR2(20)
,HIRE_DATE DATE NOT NULL
,JOB_ID VARCHAR2(10) NOT NULL
,SALARY NUMBER(8,2)
,COMMISSION_PCT NUMBER(2,2)
,MANAGER_ID NUMBER(6)
,DEPARTMENT_ID NUMBER(4)
,CONSTRAINT employees_part_pk PRIMARY KEY (employee_id, hire_date)
)
ORGANIZATION INDEX
MAPPING TABLE;
Since the mapping table has been specified on the IOT, bitmap indexes can be created on the IOT.
SQL> CREATE BITMAP INDEX employees_part_1i
2 ON employees_part (department_id)
3 NOLOGGING
3 LOCAL;
Index created.
If no mapping table is specified on the IOT, you will receive the following error when attempting to create the bitmap index:
ON employees_part (department_id)
*
ERROR at line 2:
ORA-28669: bitmap index can not be created on an IOT with no mapping table.
The Oracle error ORA-28669 still exists in Oracle 19c.
This error occurs when you attempt to create a secondary index on an Index-Organized Table (IOT) without enabling row movement.
Error Message:
ORA-28669: bitmap index is not supported on index-organized tables
Cause:
In Oracle, bitmap indexes are not supported on Index-Organized Tables (IOTs).
You may also see ORA-28669 when trying to create a secondary B-tree index on an IOT without enabling row movement.
Solution:
Before creating a secondary index on an IOT, you must enable row movement using:
ALTER TABLE IOT_SALES ENABLE ROW MOVEMENT;
Then, create the secondary index:
CREATE INDEX PROD_ID_IDX
ON IOT_SALES(PROD_ID);
Additional Notes for Oracle 19c
Bitmap indexes are still not supported on IOTs.
Row movement must be enabled before creating a secondary index.
Logical RowIDs (IOT_MAPPING_TABLE) can be used to create more flexible indexing strategies.
If you are attempting to create a bitmap index on an existing IOT with no mapping, simply alter the table.
For example,
SQL> alter table employees_part move mapping table;
Table altered.
Creating Secondary Index on IOT - Exercise
Click the link below to practice creating a secondary index on an index-organized table. Creating Secondary Index on IOT - Exercise
In the next lesson, you will learn about the limitations on index-organized tables.