Oracle Database supports several types of indexes to improve SQL query performance and data retrieval. Different index types are designed for different workloads and data access requirements.
Understanding the different types of indexes is important for Oracle SQL Developers, Oracle DBAs, and database performance tuning professionals.
Let's understand the important types of Oracle indexes with simple explanations and practical SQL examples.
1. B-Tree Index
What is a B-Tree Index?
- It is the default index type in Oracle.
- Stores indexed values in a balanced tree structure.
- Used for fast searching, filtering, and sorting.
- Suitable for both equality and range conditions.
Example: Create a B-Tree Index
CREATE INDEX idx_emp_name
ON employees(emp_name);
Example Query:
SELECT *
FROM employees
WHERE emp_name = 'Rahul';
Oracle may use the B-Tree index to locate the employee record efficiently.
2. Bitmap Index
What is a Bitmap Index?
- Stores information using bitmap representations.
- Best suited for columns with low cardinality (few distinct values).
- Commonly used in data warehouses.
- Can efficiently combine multiple filtering conditions using bitmap operations.
Example: Create a Bitmap Index
CREATE BITMAP INDEX idx_gender
ON employees(gender);
Example Query:
SELECT *
FROM employees
WHERE gender = 'M';
Bitmap indexes are useful when a column contains only a small number of distinct values, such as gender, status, or category.
3. Unique Index
What is a Unique Index?
- Ensures that indexed key values are unique.
- Prevents duplicate complete index key values.
- Commonly associated with PRIMARY KEY and UNIQUE constraints.
Example: Create a Unique Index
CREATE UNIQUE INDEX idx_emp_email
ON employees(email);
Example Query:
SELECT *
FROM employees
WHERE email = 'rahul@gmail.com';
A unique index prevents two rows from having the same non-NULL email value in this single-column example.
4. Composite Index
What is a Composite Index?
- Created on two or more columns.
- Useful when queries frequently filter using multiple columns.
- Column order is important.
- Can support queries using leading index columns.
Example: Create a Composite Index
CREATE INDEX idx_emp_dept_name
ON employees(dept_id, emp_name);
Example Query:
SELECT *
FROM employees
WHERE dept_id = 10
AND emp_name = 'Rahul';
Here, Oracle can potentially use the composite index because the query filters using both indexed columns.
5. Function-Based Index
What is a Function-Based Index?
- Created on an expression or function applied to a column.
- Useful when queries use functions in WHERE conditions.
- Can improve queries that search using UPPER(), LOWER(), or other suitable expressions.
Example: Create a Function-Based Index
CREATE INDEX idx_upper_name
ON employees(UPPER(emp_name));
Example Query:
SELECT *
FROM employees
WHERE UPPER(emp_name) = 'RAHUL';
The Function-Based Index can help Oracle search using the uppercase expression instead of calculating it for every row during a full table scan.
6. Reverse Key Index
What is a Reverse Key Index?
- Reverses the bytes of indexed key values.
- Can help reduce right-hand index block contention.
- Useful in certain high-concurrency sequential insert workloads.
- Does not support normal range scans on the reversed key.
Example: Create a Reverse Key Index
CREATE INDEX idx_emp_id
ON employees(emp_id)
REVERSE;
A Reverse Key Index can distribute sequential key insertions across different index leaf blocks instead of concentrating inserts at the rightmost leaf block.
7. Domain Index
What is a Domain Index?
- Used for specialized data types and application-specific indexing.
- Implemented using Oracle Data Cartridge interfaces.
- Commonly used for spatial data and text searching.
- Supports specialized search requirements beyond normal B-Tree indexing.
Common Examples:
- Oracle Text indexes for full-text searching.
- Spatial indexes for geographic and location-based data.
Unlike ordinary B-Tree indexes, Domain Indexes use specialized indexing implementations.
8. Invisible Index
What is an Invisible Index?
- Maintained by Oracle during DML operations.
- Ignored by the optimizer by default.
- Useful for testing an index before making it visible to normal optimizer operations.
- Can be made visible without dropping and recreating it.
Example: Create an Invisible Index
CREATE INDEX idx_emp_salary
ON employees(salary)
INVISIBLE;
Make the Index Visible:
ALTER INDEX idx_emp_salary VISIBLE;
Make the Index Invisible:
ALTER INDEX idx_emp_salary INVISIBLE;
9. Quick Interview Summary
The following table summarizes the important Oracle index types and their main purposes.
| Index Type | Main Purpose |
|---|---|
| B-Tree | General-purpose indexing. |
| Bitmap | Low-cardinality columns. |
| Unique | Prevent duplicate key values. |
| Composite | Index multiple columns. |
| Function-Based | Index expressions and functions. |
| Reverse Key | Reduce right-hand index block contention in suitable workloads. |
| Domain | Specialized data searching. |
| Invisible | Test indexes without optimizer usage by default. |
10. Important Oracle Database Interview Questions
Q1. Which is the default index type in Oracle?
Answer: B-Tree Index is the default index type in Oracle Database.
Q2. Which index is suitable for low-cardinality columns?
Answer: Bitmap Index is generally suitable for low-cardinality columns, particularly in data warehouse environments.
Q3. What is the main advantage of a Composite Index?
Answer: A Composite Index can improve queries that filter or search using multiple indexed columns.
Q4. What is a Function-Based Index?
Answer: It is an index created on an expression or function applied to a column.
Q5. What is an Invisible Index?
Answer: An Invisible Index is maintained by Oracle but is normally ignored by the optimizer when selecting execution plans.
Conclusion
Oracle Database provides different types of indexes to support different SQL query requirements and application workloads.
B-Tree indexes are commonly used for general-purpose indexing, Bitmap indexes are useful in data warehouses, Composite indexes support multiple-column searches, and Function-Based indexes help optimize queries that use expressions.
Understanding when and how to use each index type is essential for improving Oracle SQL query performance.
Keep Learning Oracle SQL and Database Performance Tuning!
No comments:
Post a Comment