Sub topic 3
Creating B-tree, unique, and composite indexes
This chapter assumes you can write basic SQL (SELECT, WHERE, JOIN, ORDER BY), create tables and insert rows in Oracle, and know that every row has a ROWID. It also assumes you know the optimizer picks an execution plan. The previous chapter described how a B-tree index is organized. This chapter covers the statements that create one, the three forms you will use most often, and the errors that appear in practice.
The default index type
In Oracle, CREATE INDEX builds a B-tree index unless you ask for something else, such as BITMAP. You do not write the word "B-tree" anywhere in the statement.
The basic form is:
CREATE INDEX index_name ON table_name (column_list);The sample table for this chapter is:
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)
);The PRIMARY KEY clause already caused Oracle to create an index. Oracle creates a unique B-tree index to enforce the key, and it names the index automatically (something like SYS_C0012345) unless you name the constraint or supply a USING INDEX clause. You can see it in the data dictionary:
SELECT index_name, uniqueness
FROM user_indexes
WHERE table_name = 'ORDERS';SELECT index_name, column_name, column_position
FROM user_ind_columns
WHERE table_name = 'ORDERS'
ORDER BY index_name, column_position;Check these views before creating any index. A column that is already the leading column of an existing index rarely needs another one.
Single-column indexes
A single-column index is the simplest form. It helps queries that filter or join on that one column.
CREATE INDEX orders_customer_ix ON orders (customer_id);A query such as WHERE customer_id = 42 can now locate the matching ROWIDs by walking the tree, instead of reading every block of the table.
Name indexes consistently. A common convention is table_columns_ix, and the name must be unique within the schema because indexes share a namespace with tables and other objects.
Unique indexes
A unique index rejects duplicate key values. You can create one in two ways.
CREATE UNIQUE INDEX orders_ext_ref_ux ON orders (email, order_date);Or you can declare a constraint and let Oracle build the index for you:
ALTER TABLE orders ADD CONSTRAINT orders_ext_ref_uk UNIQUE (email, order_date);Both enforce uniqueness. The constraint form is preferred when the rule is part of your data model, because the constraint appears in the table's definition and tools that read constraints (such as foreign keys, which need a unique or primary key to reference) can use it.
Two behaviors are worth knowing:
- NULLs. Oracle does not store a B-tree entry for a row whose indexed columns are all NULL. In a single-column index, any number of NULLs are allowed in a unique index. In a composite unique index, a row is only stored if at least one column is non-NULL, and two rows with the same non-NULL values and NULLs in the same positions count as duplicates.
- Queries on NULL. Because all-NULL rows are not in the index,
WHERE col IS NULLon a single indexed nullable column cannot use that index. Declaring the columnNOT NULL, asordersdoes for most columns, avoids this problem.
Composite indexes
A composite index (also called a concatenated index) covers two or more columns. The sample index from the topic is:
CREATE INDEX orders_cust_date_ix ON orders (customer_id, order_date);Entries are sorted by customer_id first. Within each customer, they are sorted by order_date. That sort order decides which queries can use the index efficiently.
| Query predicate | Can use `orders_cust_date_ix` well? | Reason |
|---|---|---|
| `customer_id = 42` | Yes | Uses the leading column |
| `customer_id = 42 AND order_date >= DATE '2026-01-01'` | Yes | Both columns narrow the range |
| `order_date >= DATE '2026-01-01'` | Usually not as a range scan | The leading column is not constrained |
| `customer_id = 42 ORDER BY order_date` | Yes | Entries are already in date order within a customer |
The next chapters return to column order and skip scans. For now, remember the rule: columns you compare with equality usually go first, and a column used in a range comparison goes after them.
A composite index can also serve queries that need only its columns. For example, SELECT customer_id, order_date FROM orders WHERE customer_id = 42 can be answered from the index without visiting the table at all.
The following diagram shows how a composite index orders its entries.
Useful CREATE INDEX options
You can add clauses to the statement. These are the ones you will meet most often.
CREATE INDEX orders_cust_date_ix
ON orders (customer_id, order_date)
TABLESPACE users
COMPUTE STATISTICS;TABLESPACEplaces the index segment in a specific tablespace. If omitted, Oracle uses the default tablespace of the owner.ONLINElets DML continue on the table while the index builds. Without it, building the index takes a lock that blocks changes to the table.PARALLEL nbuilds a large index with several processes. Reset the degree afterward withALTER INDEX ... NOPARALLEL, or later queries may run in parallel unexpectedly.DESCon a column stores that column in descending order. Oracle implements it as a function-based index on a hidden column, so it has the same matching rules as the function-based indexes covered later.
Oracle gathers index statistics automatically when you create an index on a populated table, so the old COMPUTE STATISTICS clause is no longer needed. Leave it out in new code.
Common errors
ORA-00955: name is already used by an existing object. An index, table, or view in the same schema has that name. Pick another name or check USER_OBJECTS.
ORA-01408: such column list already indexed. You tried to create a second index on exactly the same columns in the same order. Oracle refuses duplicates. A different column order, such as (order_date, customer_id), is allowed.
ORA-01452: cannot CREATE UNIQUE INDEX; duplicate keys found. The table already contains duplicates. Find them first:
SELECT email, order_date, COUNT(*)
FROM orders
GROUP BY email, order_date
HAVING COUNT(*) > 1;ORA-00054: resource busy and acquire with NOWAIT specified. Another session holds a lock on the table. Retry later, or use the ONLINE clause.
Over-indexing. Every index must be updated on each insert and delete, and on each update to an indexed column. A table with ten indexes does roughly ten extra index changes per insert. Create indexes for queries you know you run, and check the plan before and after.
Checking that the index is used
Creating an index does not guarantee the optimizer will use it. Confirm with a plan:
EXPLAIN PLAN FOR
SELECT order_id
FROM orders
WHERE customer_id = 42
AND order_date >= DATE '2026-01-01';
SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY);Look for INDEX RANGE SCAN on ORDERS_CUST_DATE_IX in the output. If you see TABLE ACCESS FULL, the optimizer judged a full scan cheaper. On a tiny table that is the correct choice. The chapter on reading plans covers this in detail.
Summary of the three forms
| Form | Statement shape | Main purpose |
|---|---|---|
| Single-column | `CREATE INDEX i ON t (a)` | Speed up filters and joins on one column |
| Unique | `CREATE UNIQUE INDEX i ON t (a)` or a `UNIQUE` constraint | Speed lookups and reject duplicates |
| Composite | `CREATE INDEX i ON t (a, b)` | Support predicates on several columns, and ordered retrieval |
Exercise
The orders table has a composite index on (customer_id, order_date). A developer proposes also creating CREATE INDEX orders_cust_ix ON orders (customer_id). Is the new index likely to help? What does it cost?
Show answer
Probably not. The composite index starts with customer_id, so it already supports WHERE customer_id = 42 with an index range scan. The new index would add work to every insert, delete, and update of customer_id, and it would use extra storage, with little benefit. The developer should check USER_IND_COLUMNS first and test the query plan.
Exercise
You run CREATE UNIQUE INDEX orders_ext_ref_ux ON orders (email, order_date) and get ORA-01452. What does it mean, and how do you find the cause?
Show answer
The table already holds rows with the same (email, order_date) values, so the unique index cannot be built. Run a query that groups by email, order_date with HAVING COUNT(*) > 1 to list the duplicates, fix or remove them, and run the statement again.