1. What is a Reverse Key Index in Oracle?
A Reverse Key Index is a B-Tree index that stores the bytes of the indexed values in reverse order.
It helps reduce index block contention when many users insert sequential values, such as sequence-generated IDs, at the same time.
Example:
CREATE INDEX idx_emp_rev
ON employees(employee_id)
REVERSE;
2. Why do we use a Reverse Key Index?
Reverse Key Indexes are used to:
- Reduce index block contention
- Improve concurrent INSERT performance
- Distribute index entries across multiple index blocks
- Improve scalability in high-concurrency OLTP systems
3. How does a Reverse Key Index work?
Suppose employee IDs are inserted in sequence:
1001
1002
1003
1004
Normally, Oracle inserts them into the same end of the index, which can create contention. With a Reverse Key Index, Oracle internally stores them in reverse-byte order, helping spread inserts across different index blocks.
4. How do you create a Reverse Key Index?
Syntax:
CREATE INDEX index_name
ON table_name(column_name)
REVERSE;
Example:
CREATE INDEX idx_empid_rev
ON employees(employee_id)
REVERSE;
5. When should you use a Reverse Key Index?
Use a Reverse Key Index when:
- The indexed column uses a sequence
- Many users insert data simultaneously
- The application experiences index hot spots
- The system is an OLTP application with heavy INSERT activity
Example:
INSERT INTO employees
VALUES (emp_seq.NEXTVAL, 'John', 50000);
If thousands of users insert rows concurrently, a Reverse Key Index can reduce contention.
6. When should you avoid a Reverse Key Index?
Avoid Reverse Key Indexes when queries frequently perform:
- Range searches
- BETWEEN operations
- Greater than (>)
- Less than (<)
- ORDER BY using the indexed column
These operations are generally not efficient because the key values are stored in reverse order.
7. Difference between a Normal Index and a Reverse Key Index
| Normal Index | Reverse Key Index |
|---|---|
| Stores keys in normal order | Stores keys in reverse-byte order |
| Supports range scans | Does not efficiently support range scans |
| Suitable for general queries | Suitable for high-concurrency inserts |
| Default index type | Special B-Tree index option |
8. Can a Reverse Key Index be Unique?
Yes.
Example:
CREATE UNIQUE INDEX idx_emp_rev
ON employees(employee_id)
REVERSE;
This enforces uniqueness while also reducing insert contention.
9. Can a Reverse Key Index be Composite?
Yes.
Example:
CREATE INDEX idx_emp_rev
ON employees(employee_id, department_id)
REVERSE;
The composite key is stored using reverse-key indexing.
10. Can Oracle perform Range Scans on a Reverse Key Index?
Generally, no.
Example:
SELECT *
FROM employees
WHERE employee_id BETWEEN 1000 AND 2000;
Oracle usually performs a Full Table Scan or uses another suitable index because Reverse Key Indexes are not designed for efficient range scans.
11. How do you view Reverse Key Indexes?
SELECT index_name,
index_type
FROM user_indexes;
The INDEX_TYPE column indicates the index type. You can also review the index DDL using DBMS_METADATA.GET_DDL to confirm that it was created with the REVERSE option.
12. How do you drop a Reverse Key Index?
DROP INDEX idx_emp_rev;
13. What are the advantages of a Reverse Key Index?
- Reduces index block contention
- Improves concurrent INSERT performance
- Better scalability for sequence-generated keys
- Useful in high-volume OLTP applications
14. What are the disadvantages of a Reverse Key Index?
- Does not efficiently support range scans
- Not suitable for BETWEEN, <, or > searches
- Less suitable for queries requiring ordered index access
15. Interview Example
Create a table:
CREATE TABLE employees (
employee_id NUMBER,
first_name VARCHAR2(50)
);
Create a Reverse Key Index:
CREATE INDEX idx_emp_rev
ON employees(employee_id)
REVERSE;
Insert data:
INSERT INTO employees
VALUES (1001, 'John');
INSERT INTO employees
VALUES (1002, 'David');
INSERT INTO employees
VALUES (1003, 'Scott');
The Reverse Key Index helps distribute these sequential key inserts across the index, reducing contention.
16. Real-World Example
Suppose an online banking system generates transaction IDs using a sequence.
INSERT INTO transactions
VALUES (txn_seq.NEXTVAL, 5000, SYSDATE);
Create the index:
CREATE INDEX idx_txn_rev
ON transactions(transaction_id)
REVERSE;
Thousands of users can insert new transactions simultaneously with less index block contention than a normal B-Tree index.
17. Can Oracle use a Reverse Key Index for Equality Searches?
Yes.
Example:
SELECT *
FROM employees
WHERE employee_id = 105;
Oracle can efficiently use a Reverse Key Index for equality (=) searches.
18. How do you check if Oracle is using a Reverse Key Index?
Use an execution plan.
EXPLAIN PLAN FOR
SELECT *
FROM employees
WHERE employee_id = 105;
SELECT *
FROM TABLE(DBMS_XPLAN.DISPLAY);
If the execution plan shows an index access path, Oracle is using the Reverse Key Index.
Reverse Key Indexes are best for high-concurrency inserts with sequential keys, but they are not suitable for range queries.
No comments:
Post a Comment