Test Index Changes Safely with Oracle Invisible Indexes
Test Index Changes Safely with Oracle Invisible Indexes
Purpose
An index has not shown up in a plan for months, and someone wants it gone. Dropping it is quick. Getting it back is not: you must re-create it, and for that you need the exact parameters of the original CREATE INDEX statement. If one month-end report did need that index, you find out when the report runs long.
An invisible index gives you a safer step between "keep" and "drop". The optimizer ignores the index, but Oracle still maintains it on every insert, update and delete. If nothing slows down, and the index does not back a foreign key or a unique or primary key constraint, you can drop it. If something does slow down, one ALTER INDEX ... VISIBLE statement puts it back, with no rebuild. The feature also works in reverse: you can create a new index as invisible and test it in one session before any other session can use it.
Code
1-- 1. Check whether the index is visible to the optimizer now
2SELECT INDEX_NAME, VISIBILITY FROM USER_INDEXES
3WHERE INDEX_NAME = 'IND1';
4
5-- 2. Hide the index from the optimizer (DML still maintains it)
6ALTER INDEX ind1 INVISIBLE;
7
8-- 3. In one test session only: compare the plan with and without the index
9SET SERVEROUTPUT OFF
10ALTER SESSION SET OPTIMIZER_USE_INVISIBLE_INDEXES = TRUE;
11-- run your test query here (one that used IND1), then:
12SELECT * FROM table (
13 DBMS_XPLAN.DISPLAY_CURSOR);
14ALTER SESSION SET OPTIMIZER_USE_INVISIBLE_INDEXES = FALSE;
15-- run the same test query again here, then:
16SELECT * FROM table (
17 DBMS_XPLAN.DISPLAY_CURSOR);
18
19-- 4a. The index is needed: make it visible again
20ALTER INDEX ind1 VISIBLE;
21
22-- 4b. Or create a new index as invisible from the start
23CREATE INDEX emp_ename ON emp(ename)
24 TABLESPACE users
25 STORAGE (INITIAL 20K
26 NEXT 20k)
27 INVISIBLE;
Code Breakdown
Step 1: read the VISIBILITY column
USER_INDEXES has one row for each index in your own schema. Its VISIBILITY column is a VARCHAR2(9) that holds VISIBLE or INVISIBLE. The same column is in ALL_INDEXES and DBA_INDEXES, so from a DBA account you can check another schema's indexes by querying DBA_INDEXES and adding OWNER to the filter.
Run this check before and after each change, because a plan shows only the indexes the optimizer used; it does not list an index it was not allowed to see.
Step 2: ALTER INDEX ... INVISIBLE
This statement changes whether the optimizer may use the index. Every DML statement on the table keeps updating the index as before. From this point the optimizer does not consider the index when it builds a plan, unless the session has the parameter in step 3 set to true.
To run it, the index must be in your own schema, or you must hold the ALTER ANY INDEX system privilege.
You can make a whole partitioned index invisible. You cannot make one partition invisible and leave the others visible; Oracle returns an error if you try.
Step 3: test in one session
OPTIMIZER_USE_INVISIBLE_INDEXES is a Boolean initialization parameter. Its default is false. When it is true, the optimizer treats invisible indexes as normal indexes. You can change it with ALTER SESSION or ALTER SYSTEM, and you can change it inside a pluggable database.
Set it with ALTER SESSION, so that only your test session can see the hidden index. A value set this way lasts only until the session ends. Run the query with the parameter true, then again with it false, and compare the two plans. DBMS_XPLAN.DISPLAY_CURSOR with no arguments formats the plan of the last SQL statement the session ran, so call it immediately after the query. The calling account needs SELECT or READ on V$SQL_PLAN, V$SESSION, V$SQL_PLAN_STATISTICS_ALL and V$SQL; SELECT_CATALOG_ROLE includes them. In SQL*Plus, turn SERVEROUTPUT off first. With it on, the tool makes an extra call to read the DBMS_OUTPUT buffer after your query, so DISPLAY_CURSOR reads that call instead and reports that it cannot fetch a plan. With the index invisible and the parameter false, a query that used to read the index takes another access path, such as TABLE ACCESS FULL. With the parameter true, the index access comes back, for example as an INDEX RANGE SCAN.
Set the parameter back to false at the end. If you leave it true, every later query in the same session can use every invisible index, and the plans you compare no longer show the effect of the one you hid.
Step 4a: ALTER INDEX ... VISIBLE
Oracle kept the index up to date the whole time it was invisible, so it can serve queries again as soon as you make it visible. There is no rebuild. This is the main difference from the two other ways to take an index out of use:
- Dropping it returns all extents of the index segment to the tablespace. To get it back you must re-create it.
- Marking it unusable also makes the optimizer ignore it, but DML stops maintaining it and Oracle drops the index segment. An unusable index must be rebuilt, or dropped and re-created, before it can be used again.
Step 4b: CREATE INDEX ... INVISIBLE
The INVISIBLE keyword at the end of CREATE INDEX builds the index in the hidden state. DML maintains it from the moment it exists, but no session uses it until you set the parameter in that session or make the index visible. This is how you add an index to a shared system and test it with one report before the rest of the application sees it.
Key Points
- Available from Oracle Database 11g Release 1. The
VISIBLE | INVISIBLEclause is part of bothCREATE INDEXandALTER INDEX. - An invisible index still costs DML work. Oracle maintains it like any other index, so hiding it saves nothing on inserts, updates or deletes. Only a drop removes that cost.
- A unique invisible index still enforces uniqueness. A duplicate insert still fails with
ORA-00001, whatever the parameter is set to. - An invisible index on a foreign key column still protects the child table. Oracle can still use it to check the foreign key, which avoids the table-level locking problems on the child when a parent row is deleted or updated. So a test period with the index invisible does not show what happens once it is dropped.
- Two indexes on the same columns: only one can be visible. You can build a second index on the same column list if it differs in type or another property, but only one of them can be visible at a time. Making one visible while the other is visible returns an error. Multiple indexes on the same column list need Oracle Database 12c or later.
- You cannot drop the index behind an enabled primary key or unique constraint on its own. You must disable or drop the constraint first.
Insights and Best Practices
How long to leave an index invisible
Leave it invisible for at least one full business cycle: a period that includes every month-end, quarterly and yearly job and report that touches the table. Write down the date you made the change, so the drop decision has a record behind it.
Pair the test with index usage monitoring
Before you hide the index, find out whether anything uses it. ALTER INDEX ind1 MONITORING USAGE starts tracking. In Oracle Database 19c, USER_OBJECT_USAGE then shows a USED column of YES or NO for the monitoring period, with its start and stop times. ALTER INDEX ind1 NOMONITORING USAGE ends the period. The MONITORING USAGE clause works only on an index in your own schema, and each new MONITORING USAGE resets the earlier result.
A NO result is a reason to try the invisible step. It is not proof that the index is safe to drop, because index monitoring does not record an index that Oracle uses to check foreign key values.
Keep the parameter at session level on a shared database
ALTER SYSTEM would let the optimizer in every session on the instance use every invisible index, while VISIBILITY still says INVISIBLE. On a database where someone else hid an index on purpose, that undoes their test. When you want all sessions to use one index, make it visible with ALTER INDEX.
Use visibility to swap one index for another
When two indexes cover the same columns, you can switch between them with visibility alone. Build the new index as invisible and test it in one session. Then make the old one invisible and the new one visible. Keep the old index for a while before you drop it, so you can switch back with two statements if the new plan turns out worse.
Check the result before you drop
Before the final DROP INDEX, run the step 1 query again and confirm that the index you are about to drop is the one marked INVISIBLE. For a full list of a table's indexes and their columns, see identifying indexed columns in Oracle tables. If you keep the index, see rebuilding and coalescing indexes with ALTER INDEX for its upkeep.
When to Use This Check
- Before you drop an index that usage monitoring says is unused.
- Before you add an index to a third-party or shared application, to test one module without changing plans for the rest.
- When you want to compare two plans for the same query, with and without one index.
- When you replace an index with a different type on the same columns, for example a B-tree index with a bitmap index.
Troubleshooting Common Issues
The plan did not change after ALTER INDEX ... INVISIBLE. Check the parameter first. If OPTIMIZER_USE_INVISIBLE_INDEXES is true for the session or the instance, the optimizer still sees the index. Then run the step 1 query to confirm the visibility change took effect. Last, make sure you ran DISPLAY_CURSOR immediately after the test query, and that SERVEROUTPUT is off: it shows the session's last statement, so any statement in between replaces the plan you wanted.
An error when making an index visible. If another visible index already exists on the same column list, Oracle refuses. Make the other index invisible first. An error when making a single partition invisible is expected, because visibility applies to the whole index.
Inserts still fail, or parent deletes still behave the same, with the index invisible. These are the uniqueness and foreign key cases in Key Points. To learn how the application behaves without the index, drop it on a test copy of the database.
A privilege error on ALTER INDEX. The index is in another schema and your account lacks the ALTER ANY INDEX system privilege. Connect as the owner, or have the privilege granted.
References
- ALTER INDEX (Oracle Database 19c SQL Language Reference) - the VISIBLE and INVISIBLE clause, the UNUSABLE clause, and the privileges ALTER INDEX and MONITORING USAGE need
- CREATE INDEX (Oracle Database 19c SQL Language Reference) - what an invisible index is: maintained by DML, ignored by the optimizer unless the parameter is TRUE
- Managing Indexes (Oracle Database 19c Administrator's Guide) - the source of the step 1, 2, 4a and 4b statements (the guide's IND1 example name in place of its
indexplaceholder), plus unusable indexes, multiple indexes on one column list, usage monitoring and dropping - OPTIMIZER_USE_INVISIBLE_INDEXES (Oracle Database 19c Reference) - the parameter's type, default, values and how it can be modified
- ALL_INDEXES (Oracle Database 19c Reference) - the VISIBILITY column shared by the USER_, ALL_ and DBA_INDEXES views
- DBMS_XPLAN (Oracle Database 19c PL/SQL Packages and Types Reference) - the step 3 DISPLAY_CURSOR query and its default of the session's last executed statement
- ALTER SESSION (Oracle Database 19c SQL Language Reference) - the SET clause used in step 3, and that a parameter set this way lasts only for the current session
- Common GATHER_PLAN_STATISTICS confusion (connor-mcdonald.com) - why SERVEROUTPUT must be off before DISPLAY_CURSOR in SQL*Plus: the extra DBMS_OUTPUT call becomes the session's last statement
- Invisible Indexes in Oracle Database 11g Release 1 (oracle-base.com) - Tim Hall's walkthrough of the session-level parameter test, with plans before and after, and the 11g Release 1 start of the feature
- Visible Invisible Indexes (richardfoote.wordpress.com) - Richard Foote on why making an index invisible is not the same as dropping it, index monitoring missing foreign key checks, and uniqueness still enforced by an invisible index
- Indexing Foreign Key Constraints With Invisible Indexes (richardfoote.wordpress.com) - Richard Foote's Oracle 12c demo of an invisible B-tree index still policing a foreign key beside a visible bitmap index