In Oracle Database, when an index becomes UNUSABLE, Oracle cannot use that index to retrieve data until the index is rebuilt or recreated.
The actual table data is not deleted or affected. Only the index becomes unavailable for normal query access.
Let's understand this concept with a practical example.
1. What Happens When an Index Becomes UNUSABLE?
Suppose you have an employee table containing 10 lakh records.
Step 1: Create an employee table
CREATE TABLE employees (
emp_id NUMBER,
emp_name VARCHAR2(100),
salary NUMBER
);
Step 2: Create an index on EMP_ID
CREATE INDEX idx_emp_id
ON employees(emp_id);
Initially, the index status will be VALID.
Step 3: Check the index status
SELECT index_name, status
FROM user_indexes
WHERE index_name = 'IDX_EMP_ID';
| INDEX_NAME | STATUS |
|---|---|
| IDX_EMP_ID | VALID |
Step 4: Make the index UNUSABLE
ALTER INDEX idx_emp_id UNUSABLE;
Check the status again:
SELECT index_name, status
FROM user_indexes
WHERE index_name = 'IDX_EMP_ID';
| INDEX_NAME | STATUS |
|---|---|
| IDX_EMP_ID | UNUSABLE |
2. What Happens When You Execute SELECT?
Consider the following SQL query:
SELECT *
FROM employees
WHERE emp_id = 101;
What happens when you execute this query?
- Oracle cannot use the unusable index.
- Oracle may perform a Full Table Scan instead.
- The query can still return the correct result.
- Query performance may decrease, especially for large tables.
3. What Happens When You Execute INSERT, UPDATE and DELETE?
This is one of the most important Oracle DBA interview questions.
When an index becomes unusable, DML operations may fail if Oracle needs to maintain that index.
| Operation | What Happens? |
|---|---|
| SELECT | Cannot use the unusable index. |
| INSERT | Generally fails with ORA-01502 when index maintenance is required. |
| UPDATE | May fail with ORA-01502 if indexed data is affected. |
| DELETE | May fail with ORA-01502 when index maintenance is required. |
Example:
INSERT INTO employees
VALUES (101, 'Rahul', 50000);
Possible error:
ORA-01502: index 'IDX_EMP_ID' or partition of such
index is in unusable state
4. How to Fix an Unusable Index?
To fix an unusable index, you can rebuild it using the ALTER INDEX REBUILD command.
Step 1: Rebuild the index
ALTER INDEX idx_emp_id REBUILD;
Step 2: Check the index status
SELECT index_name, status
FROM user_indexes
WHERE index_name = 'IDX_EMP_ID';
Expected output:
| INDEX_NAME | STATUS |
|---|---|
| IDX_EMP_ID | VALID |
5. How Does an Index Become UNUSABLE?
Some common scenarios include:
- A DBA manually makes the index unusable.
- Partition maintenance operations affect an index partition.
- Certain bulk-loading operations use options that leave indexes unusable.
- Some index maintenance operations fail.
For example, consider a partitioned sales table:
ALTER TABLE sales
TRUNCATE PARTITION p2025
UPDATE GLOBAL INDEXES;
The UPDATE GLOBAL INDEXES clause helps maintain global indexes during the partition maintenance operation.
6. Difference Between UNUSABLE and INVISIBLE Indexes
Oracle supports both UNUSABLE and INVISIBLE index states. However, they work differently.
| UNUSABLE Index | INVISIBLE Index |
|---|---|
| Cannot be used by the optimizer. | Normally ignored by the optimizer. |
| Cannot be maintained normally by DML. | Maintained during DML. |
| Needs rebuilding. | Can be made visible again. |
| May cause DML errors. | Does not normally cause DML errors. |
To make an index invisible:
ALTER INDEX idx_emp_id INVISIBLE;
To make it visible again:
ALTER INDEX idx_emp_id VISIBLE;
7. Important Oracle Interview Question
Question: If an index becomes UNUSABLE, will the SELECT query fail?
Answer:
Generally, no. Oracle can perform a Full Table Scan and retrieve the records without using the unusable index, provided no other query dependency prevents execution.
However, DML operations can fail with ORA-01502 when they need to maintain that unusable index.
Quick Revision: Remember These Three Points
- UNUSABLE index: Cannot be used for normal index access.
- Table data: Remains safe and available.
- REBUILD: Restores the index to a usable state.
Conclusion: An unusable index affects index availability, not the actual table records. As an Oracle DBA, you should regularly monitor index status and rebuild unusable indexes when required.
Happy Learning! Keep Practicing Oracle SQL and DBA Concepts.
No comments:
Post a Comment