You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
Indexes are the primary mechanism for speeding up query performance in Oracle Database. Like an index in a book, a database index allows Oracle to find rows quickly without scanning every row in a table. This lesson covers index types, the Oracle optimiser, and essential performance tuning techniques.
An index is a database object that stores a sorted copy of selected column values along with pointers (ROWIDs) to the corresponding table rows.
Without an index: Oracle performs a full table scan — reads every row. With an index: Oracle performs an index lookup — jumps directly to matching rows.
The most common index type — a balanced tree structure:
CREATE INDEX emp_lastname_idx ON employees(last_name);
=) and range (<, >, BETWEEN) queriesAn index on multiple columns:
CREATE INDEX emp_dept_sal_idx ON employees(department_id, salary);
department_id alone can use this indexsalary alone typically cannot (unless index skip scan is used)Enforces uniqueness:
CREATE UNIQUE INDEX emp_email_uk ON employees(email);
Stores bitmaps instead of row pointers — ideal for low-cardinality columns:
CREATE BITMAP INDEX emp_gender_bm ON employees(gender);
Indexes the result of an expression:
CREATE INDEX emp_upper_name_idx ON employees(UPPER(last_name));
Allows this query to use the index:
SELECT * FROM employees WHERE UPPER(last_name) = 'SMITH';
Reverses the bytes of the key — distributes sequential inserts across the index:
CREATE INDEX order_id_rev_idx ON orders(order_id) REVERSE;
Useful for avoiding hot blocks when many sessions insert sequential values.
The table data is stored within the index structure itself:
CREATE TABLE lookup_codes (
code_id NUMBER,
code_desc VARCHAR2(100),
CONSTRAINT lookup_pk PRIMARY KEY (code_id)
) ORGANIZATION INDEX;
Best for small lookup tables accessed primarily by primary key.
| Index Type | Best For |
|---|---|
| B-Tree | High-cardinality, OLTP queries |
| Composite | Multi-column filters, covering indexes |
| Bitmap | Low-cardinality, data warehouse, complex AND/OR |
| Function-Based | Queries using functions on columns |
| Reverse Key | Sequential inserts (avoids hot blocks) |
| IOT | Small lookup tables, primary key access |
Oracle uses a Cost-Based Optimiser (CBO) that evaluates multiple execution plans and chooses the one with the lowest estimated cost.
The CBO depends on accurate statistics:
-- Gather statistics for a table
EXEC DBMS_STATS.GATHER_TABLE_STATS('HR', 'EMPLOYEES');
-- Gather statistics for an entire schema
EXEC DBMS_STATS.GATHER_SCHEMA_STATS('HR');
-- Check when statistics were last gathered
SELECT table_name, last_analyzed, num_rows
FROM user_tables;
Tip: Stale statistics are a leading cause of poor performance. Oracle auto-gathers statistics by default, but verify after large data loads.
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.