Sub topic 3
Creating B-tree, Unique, and Composite Indexes
Assume you know basic SELECT and WHERE clauses, can create Oracle tables, and understand that the optimizer chooses an execution plan. This chapter focuses on defining indexes and selecting their columns.
Creating a B-tree index
An index is a separate database structure that stores column values in an ordered form and associates them with table rows. Oracle can use that structure to find matching rows without inspecting every row in the table.
A B-tree index is Oracle's general-purpose index type. It works well when queries search for particular values or ranges, such as customer IDs, dates, and order numbers. CREATE INDEX creates a B-tree index unless you specify another index type.
CREATE TABLE orders (
order_id NUMBER PRIMARY KEY,
customer_id NUMBER NOT NULL,
status VARCHAR2(10) NOT NULL,
order_date DATE NOT NULL,
email VARCHAR2(100)
);
CREATE INDEX orders_customer_ix
ON orders (customer_id);The primary key causes Oracle to enforce uniqueness for order_id. Oracle normally creates or uses a unique index to support that constraint. The explicitly created index supports searches by customer_id.
Indexes have a cost. Oracle must maintain an index when a row is inserted, deleted, or when an indexed value changes. Each index also consumes storage. Add indexes for important access paths, not for every column.
Creating unique indexes
A unique index prevents two non-null rows from having the same indexed key. Use one when the database must enforce uniqueness directly through an index.
CREATE UNIQUE INDEX orders_email_uix
ON orders (email);This index rejects a second row with the same non-null email. Oracle allows multiple null values because a null is not equal to another null for uniqueness purposes.
In many applications, a unique constraint expresses the business rule more clearly:
ALTER TABLE orders
ADD CONSTRAINT orders_email_uk UNIQUE (email);A unique constraint may use an existing suitable index or cause Oracle to create one. Prefer a constraint when the main purpose is to state and enforce a data rule. Use CREATE UNIQUE INDEX when you specifically need an index with uniqueness behavior but do not want to define a constraint.
Creating composite indexes
A composite index contains multiple columns. The order is part of the index design.
CREATE INDEX orders_cust_date_ix
ON orders (customer_id, order_date);This index is ordered first by customer_id, then by order_date within each customer. It can support queries that constrain customer_id, including queries that also constrain order_date.
SELECT order_id
FROM orders
WHERE customer_id = 42
AND order_date >= DATE '2026-01-01';The same index is usually less useful for a query that filters only by order_date, because that column is not the leading column. A separate index beginning with order_date might be needed if that access path matters.
| Index definition | Commonly useful for |
|---|---|
| `(customer_id)` | One customer lookup |
| `(customer_id, order_date)` | One customer and a date range |
| `(order_date, customer_id)` | A date range, then customer filtering |
A composite index can also help an ORDER BY when the requested order matches the index's column order and the predicates allow that access path. Test the actual workload rather than relying only on column cardinality, which is the number of distinct values.
flowchart TD
A[Query predicates] --> B{Leading indexed column constrained?}
B -->|Yes| C[Composite B-tree is a candidate]
B -->|No| D[Consider another index or full table scan]
C --> E[Optimizer compares estimated costs]
E --> F[Chosen execution plan]Exercise
Which index is a better starting point for queries that filter by customerid and then search orderdate ranges: (orderdate, customerid) or (customerid, orderdate)?
Show answer
(customer_id, order_date), because customer_id is the leading column and the date range can then be searched within that customer's entries.