In Oracle Database, you can check whether an index is being used by SQL queries using different methods.
The two commonly used methods are:
- Using V$OBJECT_USAGE / USER_OBJECT_USAGE for index usage monitoring.
- Using EXPLAIN PLAN and DBMS_XPLAN.DISPLAY_CURSOR to analyze execution plans.
In this tutorial, we will understand all three methods with practical SQL examples.
1. Check Index Usage Using V$OBJECT_USAGE
Oracle provides the USER_OBJECT_USAGE view to check whether an index has been used during the monitoring period.
Suppose we have an EMPLOYEE table with an index named IDX_EMP_ID.
Step 1: Enable Index Monitoring
Execute the following statement to enable index usage monitoring:
ALTER INDEX IDX_EMP_ID MONITORING USAGE;
This statement enables monitoring for the specified index.
Step 2: Execute a SQL Query
Run a query that may use the index:
SELECT *
FROM EMPLOYEE
WHERE EMP_ID = 101;
If Oracle's optimizer chooses the index, its usage will be recorded.
Step 3: Check Whether the Index Is Used
Execute the following query:
SELECT
INDEX_NAME,
TABLE_NAME,
MONITORING,
USED
FROM USER_OBJECT_USAGE
WHERE INDEX_NAME = 'IDX_EMP_ID';
Sample Output
| INDEX_NAME | TABLE_NAME | MONITORING | USED |
|---|---|---|---|
| IDX_EMP_ID | EMPLOYEE | YES | YES |
Understanding the Output
- MONITORING = YES: Index usage monitoring is enabled.
- USED = YES: The index was used at least once during the monitoring period.
- USED = NO: The index was not recorded as used during the monitoring period.
2. Check Index Usage Using EXPLAIN PLAN
EXPLAIN PLAN is another useful method to check whether Oracle's optimizer plans to use an index.
It displays the estimated execution plan for a SQL statement.
Step 1: Generate the Execution Plan
Execute the following statement:
EXPLAIN PLAN FOR
SELECT *
FROM EMPLOYEE
WHERE EMP_ID = 101;
Step 2: Display the Execution Plan
Execute:
SELECT *
FROM TABLE(DBMS_XPLAN.DISPLAY);
Example Execution Plan Output
---------------------------------------------------------
| Id | Operation | Name |
---------------------------------------------------------
| 0 | SELECT STATEMENT | |
| 1 | TABLE ACCESS BY INDEX ROWID | EMPLOYEE |
| 2 | INDEX UNIQUE SCAN | IDX_EMP_ID |
---------------------------------------------------------
Understanding Important Execution Plan Operations
- INDEX UNIQUE SCAN: Oracle accesses a unique index entry to locate a row.
- INDEX RANGE SCAN: Oracle scans a range of index entries.
- INDEX FULL SCAN: Oracle scans all entries in an index.
- TABLE ACCESS FULL: Oracle reads the table using a full table scan.
- TABLE ACCESS BY INDEX ROWID: Oracle accesses table rows using ROWIDs obtained from an index.
3. Check the Actual Execution Plan Using DBMS_XPLAN.DISPLAY_CURSOR
EXPLAIN PLAN displays the optimizer's estimated execution plan. To analyze the execution plan of an executed SQL statement, you can use DBMS_XPLAN.DISPLAY_CURSOR.
Step 1: Execute the Query with GATHER_PLAN_STATISTICS
SELECT /*+ GATHER_PLAN_STATISTICS */
*
FROM EMPLOYEE
WHERE EMP_ID = 101;
The GATHER_PLAN_STATISTICS hint collects runtime plan statistics for this execution.
Step 2: Display the Actual Execution Plan
SELECT *
FROM TABLE(
DBMS_XPLAN.DISPLAY_CURSOR(
NULL,
NULL,
'ALLSTATS LAST'
)
);
This displays the execution plan and available runtime statistics for the last executed cursor in your session.
What Should You Check?
- Operation
- Name
- Starts
- E-Rows (estimated rows)
- A-Rows (actual rows)
- Buffers
4. Difference Between These Methods
| Method | Purpose |
|---|---|
| USER_OBJECT_USAGE | Checks whether an index was used during monitoring. |
| EXPLAIN PLAN | Displays the optimizer's estimated execution plan. |
| DBMS_XPLAN.DISPLAY_CURSOR | Displays the execution plan of an executed SQL cursor and available runtime statistics. |
5. Important Oracle Index Interview Question
Question: How do you check whether an index is being used in Oracle?
I use EXPLAIN PLAN and DBMS_XPLAN.DISPLAY_CURSOR to check whether Oracle is performing an index scan or a full table scan.
I can also enable index usage monitoring using:
ALTER INDEX IDX_EMP_ID MONITORING USAGE;
Then I check the index usage using USER_OBJECT_USAGE:
SELECT
INDEX_NAME,
TABLE_NAME,
USED
FROM USER_OBJECT_USAGE
WHERE INDEX_NAME = 'IDX_EMP_ID';
6. Important Points to Remember
- An index may exist on a table, but Oracle does not always use it.
- Oracle's optimizer decides whether to use an index or perform a full table scan.
- Table statistics and index statistics influence execution plan decisions.
- Index selectivity and query conditions can affect index usage.
- EXPLAIN PLAN shows an estimated execution plan, not proof of actual execution.
- DBMS_XPLAN.DISPLAY_CURSOR helps analyze the executed SQL cursor.
- USER_OBJECT_USAGE helps monitor index usage over a monitoring period.
Conclusion
Checking index usage is an important activity for Oracle DBAs and performance tuning professionals.
Oracle provides different methods to identify whether an index is being used:
- USER_OBJECT_USAGE: Monitors index usage.
- EXPLAIN PLAN: Displays the estimated execution plan.
- DBMS_XPLAN.DISPLAY_CURSOR: Analyzes the execution plan of an executed SQL statement.
Understanding these methods will help you troubleshoot SQL performance issues, analyze execution plans, and make better index management decisions.
Related Oracle DBA Topics:
- Oracle Index Types
- Oracle Index Monitoring
- Oracle Execution Plan
- Oracle Performance Tuning
- Oracle DBMS_XPLAN
- Oracle Full Table Scan vs Index Scan
No comments:
Post a Comment