Oracle Indexes

Oracle Indexes: Types, Usage, and Management

Indexes are critical database objects that improve the speed of data retrieval operations. Without indexes, Oracle would have to perform a full table scan to find the requested data, which becomes increasingly inefficient as table size grows (Oracle, 2025a). This article examines the different index types available in Oracle Database, their creation syntax, and the mechanisms the Cost-Based Optimizer (CBO) uses to determine when an index should be used.


1. B-Tree Indexes

B-Tree (Balanced Tree) indexes are the most common type of index in Oracle. A B-Tree index is an ordered list of values divided into ranges, storing the index key value and the corresponding ROWID in a tree structure (Oracle, 2025a). When an index is created, a root block is created, followed by branch blocks and leaf blocks. Each branch block holds the range of data its leaf blocks contain, and the root block holds the range of its branches.

A key advantage of B-Tree indexes is that all leaf blocks are at the same depth, meaning retrieval of any record takes approximately the same amount of time (Oracle, 2025a). The tree automatically stays balanced through block splitting when new data is inserted.

CREATE INDEX scott.exp_idx 
ON scott.example(name) 
TABLESPACE TOOLS;

Compressed B-Tree Indexes

Compressed B-Tree indexes are designed for data warehouse environments. Duplicate occurrences of the same index key value are eliminated, reducing storage requirements (Oracle, 2025a). For each key value, a list of ROWIDs is maintained. The COMPRESS keyword is used during creation.

CREATE INDEX idx_compressed 
ON sales_data(region, product_id) 
COMPRESS;

2. Bitmap Indexes

Bitmap indexes are most appropriate for columns with low distinct cardinality—that is, columns with few unique values relative to the total number of rows (Oracle, 2025a). Examples include gender (M/F) or marital status. Instead of storing a ROWID for each row, a bitmap index creates a binary map where each bit corresponds to a possible ROWID (Oracle, 2026a). If the bit is set (1), the row contains the key value.

Bitmap indexes are space-efficient for low-cardinality data and are particularly useful in data warehousing environments where queries frequently use AND and OR operators. However, they are not suitable for tables with high DML activity (Oracle, 2026a).

CREATE BITMAP INDEX idx_gender 
ON emp_data(gender) 
TABLESPACE TOOLS;

Note: A bitmap index can have a maximum of 30 columns, and it cannot be created as a unique index (Oracle, 2026a).


3. Function-Based Indexes

Function-based indexes are created on columns where a function is typically applied in queries. Without a function-based index, applying a function to an indexed column (e.g., UPPER(ename)) causes Oracle to ignore the index (Oracle, 2025a). A function-based index stores the computed function result, allowing the optimizer to use the index for queries that use the same function.

CREATE INDEX idx_upper_ename 
ON emp(UPPER(ename));

4. Reverse Key Indexes (RKI)

Reverse key indexes are a special type of B-Tree index designed for columns containing sequential numbers, such as values generated by a sequence (Oracle, 2025a). In a standard B-Tree index on a sequential column, inserts concentrate on the rightmost leaf block, creating a "hot block" contention point. Reverse key indexes solve this by reversing the bytes of each index key, distributing inserts across many blocks (Oracle, 2025a).

CREATE INDEX idx_reverse 
ON emp_table(firstname, lastname) 
REVERSE;

Restriction: The REVERSE keyword cannot be specified for bitmap indexes or secondary indexes on index-organized tables (Oracle, 2024).


5. Index-Organized Tables (IOT)

An index-organized table differs fundamentally from a heap-organized table: the table data itself is stored within the B-Tree index structure (Oracle, 2025b). Instead of maintaining separate storage for the table and its index, an IOT maintains a single B-Tree index containing both the encoded key value and the associated column values for the corresponding row (Oracle, 2025b).

Each B-Tree index entry contains <primary_key_value, non_primary_key_column_values>. Because the data is the index, access by primary key is extremely fast.

CREATE TABLE employees_iot (
    employee_id NUMBER PRIMARY KEY,
    last_name VARCHAR2(50),
    salary NUMBER
)
ORGANIZATION INDEX 
TABLESPACE USERS
PCTTHRESHOLD 20
OVERFLOW TABLESPACE USERS_OVERFLOW;

Features and Restrictions of IOTs:

  • A primary key must be specified.
  • Secondary indexes on IOTs use logical ROWIDs (based on the primary key) rather than physical ROWIDs (Oracle, 2025b).
  • If a bitmap index is created on an IOT, a mapping table is required (Oracle, 2025b).
  • UNIQUE constraints cannot be specified, but triggers are allowed.
  • IOTs cannot be stored in a cluster.

6. Application Domain Indexes

Oracle provides extensible indexing to support indexes on complex data types such as documents, spatial data, images, and video (Oracle, 2025a). With extensible indexing, application-specific index management routines are encapsulated as an indextype schema object, and a domain index is defined on table columns.

The application software, called a cartridge, controls the structure and content of the domain index. The Oracle server interacts with the application to build, maintain, and search the index (Oracle, 2025a).


7. How Oracle Decides to Use an Index

The Cost-Based Optimizer (CBO) determines whether an index will improve query performance. The CBO evaluates multiple factors, including selectivity, statistics, and the cost of alternative access paths (Oracle, 2026b).

When the Optimizer Uses an Index:

  • Highly selective predicates (e.g., WHERE emp_name = 'John') benefit from index access.

When the Optimizer Ignores an Index:

  • No WHERE clause: A query such as SELECT * FROM emp requires all rows and uses a full table scan (Oracle, 2026b).
  • Unindexed column in WHERE clause: WHERE dept_no = 5 cannot use an index on emp_name.
  • Function applied to indexed column: WHERE SUBSTR(emp_name,1,4) = 'XYZW' ignores the index unless a function-based index exists.
  • Unselective query: If most table blocks must be accessed, a full table scan is cheaper due to multiblock I/O (Oracle, 2026b).
  • Stale statistics: The optimizer may underestimate the benefit of an index (Oracle, 2026b).
  • Small table: A full table scan may be cheaper than an index range scan (Oracle, 2026b).
  • Leading edge not used: For a composite index on (first_name, last_name), a query filtering only on last_name may not use the index (Oracle, 2026b).

8. Rebuilding and Managing Indexes

The ALTER INDEX ... REBUILD statement reorganizes or compacts an existing index or changes its storage characteristics. The rebuild uses the existing index as the basis for the new one and is typically faster than dropping and re-creating (Oracle, 2026c).

-- Standard rebuild
ALTER INDEX idx_emp_name REBUILD;

-- Rebuild into a different tablespace
ALTER INDEX idx_emp_name REBUILD TABLESPACE NEW_INDEX_TS;

-- Online rebuild (allows DML during rebuild)
ALTER INDEX idx_emp_name REBUILD ONLINE;

The ONLINE keyword allows DML operations on the base table during the rebuild, minimizing downtime. However, parallel DML is not supported during online index building (Oracle, 2026c).

Gathering Index Statistics

Index statistics can be gathered using the ANALYZE INDEX command or the DBMS_STATS package. The VALIDATE STRUCTURE option populates the INDEX_STATS view with statistics including leaf rows and blocks (LF_ROWS, LF_BLKS), branch rows and blocks (BR_ROWS, BR_BLKS), deleted leaf rows (DEL_LF_ROWS), and used space (USED_SPACE) (Oracle, 2025a). These statistics help determine if an index should be rebuilt.

ANALYZE INDEX idx_emp_name VALIDATE STRUCTURE;
SELECT * FROM INDEX_STATS;

9. Controlling Index Usage with Hints

Oracle provides optimizer hints to influence the CBO's decision regarding index usage (Oracle, 2021).

  • NO_INDEX: Instructs the optimizer to avoid using a specific index.
  • FULL: Forces a full table scan instead of an index scan.
  • INDEX: Forces the optimizer to use a specific index.
-- Force full table scan
SELECT /*+ FULL(emp) */ * FROM emp WHERE emp_name = 'John';

-- Force index usage
SELECT /*+ INDEX(emp idx_emp_name) */ * FROM emp WHERE emp_name = 'John';

References

Oracle. (2021). SQL tuning guide: Influencing the optimizer (18c). Oracle Help Center. https://docs.oracle.com/en/database/oracle/oracle-database/18/tgsql/influencing-the-optimizer.html

Oracle. (2024). SQL language reference: CREATE INDEX (19c). Oracle Help Center. https://docs.oracle.com/en/database/oracle/oracle-database/19/sqlrf/CREATE-INDEX.html

Oracle. (2025a). Database concepts: Indexes and index-organized tables (21c). Oracle Help Center. https://docs.oracle.com/en/database/oracle/oracle-database/21/cncpt/indexes-and-index-organized-tables.html

Oracle. (2025b). Database concepts: Index-organized tables (12.2). Oracle Help Center. https://docs.oracle.com/en/database/oracle/oracle-database/12.2/cncpt/indexes-and-index-organized-tables.html

Oracle. (2026a). SQL language reference: CREATE INDEX (BITMAP) (19c). Oracle Help Center. https://docs.oracle.com/en/database/oracle/oracle-database/19/sqlrf/CREATE-INDEX.html

Oracle. (2026b). SQL tuning guide: Optimizer access paths (26). Oracle Help Center. https://docs.oracle.com/en/database/oracle/oracle-database/26/tgsql/optimizer-access-paths.html

Oracle. (2026c). SQL language reference: ALTER INDEX (26). Oracle Help Center. https://docs.oracle.com/en/database/oracle/oracle-database/26/sqlrf/ALTER-INDEX.html

No comments:

Post a Comment