Sub topic 14
Testing and Maintaining Indexes
This chapter assumes you know basic SELECT and WHERE clauses, can create Oracle tables and indexes, and understand that the optimizer chooses an execution plan. The examples use the orders table and indexes from earlier chapters.
Test an Index Without Removing It
An index can help one query while adding work to every insert, update, and delete. Before removing an index, test the workload without allowing the optimizer to use it.
An invisible index remains stored and maintained, but the optimizer normally ignores it. This makes the change reversible.
ALTER INDEX orders_cust_date_ix INVISIBLE;
-- Run representative queries and compare their plans and response times.
ALTER INDEX orders_cust_date_ix VISIBLE;Make the index visible again when the test is complete. Test more than one query. An index may support several statements, joins, or ordering operations.
A plan change alone does not prove that performance became better or worse. Compare logical reads, elapsed time, rows returned, and plan stability under realistic data volumes.
You can also enable invisible indexes for a controlled session when you want to test a query that uses them:
ALTER SESSION SET optimizer_use_invisible_indexes = TRUE;
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);Reset the session setting or end the session after the test.
Gather Statistics Before Judging a Plan
Optimizer statistics describe the table, columns, and indexes. They include information such as approximate row counts and data distribution. Stale statistics can cause the optimizer to choose a full table scan when an index would be useful, or choose an index when scanning the table would be cheaper.
Gather table statistics after a substantial data change or load:
BEGIN
DBMS_STATS.GATHER_TABLE_STATS(
ownname => USER,
tabname => 'ORDERS',
cascade => TRUE,
estimate_percent => DBMS_STATS.AUTO_SAMPLE_SIZE
);
END;
/cascade => TRUE allows Oracle to gather related index statistics as well. Use the actual schema name instead of USER when the table belongs to another schema and you have the required privileges.
Rebuild Only for a Reason
A rebuild creates a new index structure and replaces the old one. It can be appropriate when an index is marked UNUSABLE, after certain partition maintenance operations, or when an investigation shows a specific storage problem.
ALTER INDEX orders_cust_date_ix REBUILD;Do not rebuild indexes on a schedule just because they are old. Normal deletes do not automatically mean that rebuilding will improve query performance. Rebuilding also requires space, generates I/O, and can affect concurrent work. Check the index status and measure the workload before choosing this operation.
If the index is unusable, rebuild it before relying on queries that need it:
SELECT index_name, status
FROM user_indexes
WHERE index_name = 'ORDERS_CUST_DATE_IX';A Practical Maintenance Cycle
Use this sequence when an index seems ineffective:
Choosing the Safest Action
| Observation | Appropriate first action |
|---|---|
| Plan looks wrong after a large load | Gather current statistics |
| Index is invisible during a controlled test | Compare representative workload results |
| Index status is `UNUSABLE` | Rebuild it before use |
| Query is slow but the index is valid | Inspect predicates, statistics, and actual plan |
Exercise
Why is making an index invisible safer than dropping it during an initial test?
Show answer
The index remains available and maintained, so you can restore optimizer visibility without recreating the index.