What Is a Composite Index in Oracle Database?

A Composite Index in Oracle Database is an index created on two or more columns of a table.

In simple words, instead of creating an index on a single column, we create one index using multiple columns.

It is mainly used to improve SQL query performance when we frequently search or filter data using multiple columns.

Important: A Composite Index is also called a Concatenated Index because it contains multiple indexed columns.

1. Example of a Composite Index

Suppose we have an EMPLOYEE table containing employee details.

Employee Table:

EMP_ID EMP_NAME DEPARTMENT_ID SALARY
101Ravi1030000
102Kumar2040000
103Priya1035000
104Arun3045000
105Raj2050000

Suppose we frequently search employees using both DEPARTMENT_ID and SALARY.

We can create a Composite Index on these two columns.

SQL Query:

CREATE INDEX idx_dept_salary
ON employee(department_id, salary);

Explanation:

  • idx_dept_salary is the index name.
  • employee is the table name.
  • department_id is the first indexed column.
  • salary is the second indexed column.

This is called a Composite Index because it contains two columns.

2. How Does a Composite Index Work?

Consider the following SQL query:

SELECT *
FROM employee
WHERE department_id = 10
AND salary = 30000;

Oracle can use the Composite Index to locate matching records efficiently.

How Oracle Searches the Index

The index is organized using the indexed column values in the specified column order.

For our example, the index is created using:

CREATE INDEX idx_dept_salary
ON employee(department_id, salary);

Conceptually, the index entries are ordered using DEPARTMENT_ID first and SALARY second.

DEPARTMENT_ID SALARY EMPLOYEE
1030000Ravi
1035000Priya
2040000Kumar
2050000Raj
3045000Arun

When we execute the query, Oracle can navigate the index using DEPARTMENT_ID and SALARY to locate the matching index entry.

The index entry contains a ROWID that Oracle can use to retrieve the actual table row.

Remember: Oracle stores the indexed column values in the index along with ROWIDs. A Composite Index stores multiple indexed column values in the specified order.

3. Important Concept: Column Order Matters

This is one of the most important concepts in Composite Indexes.

Suppose we create the following index:

CREATE INDEX idx_dept_salary
ON employee(department_id, salary);

The column order is:

  1. DEPARTMENT_ID
  2. SALARY

Here, DEPARTMENT_ID is called the Leading Column because it is the first column in the index.

Let's Understand With Examples

SQL Condition Index Usage
WHERE department_id = 10 Yes, leading-column access is possible.
WHERE department_id = 10 AND salary = 30000 Yes, both indexed columns can be used.
WHERE salary = 30000 Generally not an efficient leading-column lookup.
WHERE department_id = 10 AND salary > 30000 Yes, both columns can help with index access.

Why?

Because DEPARTMENT_ID is the leading column (first column) of the index.

Oracle generally uses the leading column to navigate a Composite Index efficiently.

Interview Tip: Oracle may also use an Index Skip Scan when a query does not specify the leading column. However, whether this access path is efficient depends on data distribution, statistics and the optimizer's cost estimates.

4. What Happens If We Change the Column Order?

Let's create another Composite Index with the columns in reverse order.

CREATE INDEX idx_salary_dept
ON employee(salary, department_id);

Now the index column order is:

  1. SALARY
  2. DEPARTMENT_ID

Consider the following queries:

SQL Condition Index Usage
WHERE salary = 30000 Leading-column access is possible.
WHERE salary = 30000 AND department_id = 10 Both columns can be used.
WHERE department_id = 10 Generally not an efficient leading-column lookup.

This demonstrates why choosing the correct column order is important when creating Composite Indexes.

5. How to Check Composite Index Columns in Oracle?

We can use the USER_IND_COLUMNS data dictionary view to identify the columns used in an index and their positions.

SQL Query:

SELECT index_name,
       table_name,
       column_name,
       column_position
FROM user_ind_columns
WHERE table_name = 'EMPLOYEE'
ORDER BY index_name, column_position;

Example Output:

INDEX_NAME COLUMN_NAME COLUMN_POSITION
IDX_DEPT_SALARY DEPARTMENT_ID 1
IDX_DEPT_SALARY SALARY 2

The COLUMN_POSITION column indicates the order of the indexed columns.

6. Types of Composite Indexes

Composite Indexes can be created as Unique or Non-Unique Indexes.

1. Composite Unique Index

A Composite Unique Index prevents duplicate combinations of indexed column values.

SQL Query:

CREATE UNIQUE INDEX idx_dept_emp
ON employee(department_id, emp_name);

Here, the combination of DEPARTMENT_ID and EMP_NAME must be unique for non-NULL key combinations.

2. Composite Non-Unique Index

A Composite Non-Unique Index allows duplicate combinations of indexed column values.

SQL Query:

CREATE INDEX idx_dept_salary
ON employee(department_id, salary);

Here, multiple employees can have the same department ID and salary.

7. Advantages and Disadvantages of Composite Indexes

Advantages

  • Improves performance for queries filtering on multiple columns.
  • Can reduce unnecessary table scanning.
  • Can help improve JOIN performance.
  • Can support queries using the leading column.
  • Can improve sorting and grouping operations in suitable cases.
  • Can support index-only access when all required columns are available in the index.

Disadvantages

  • Requires additional storage space.
  • INSERT, UPDATE and DELETE operations may become slower.
  • Column order must be chosen carefully.
  • Unnecessary Composite Indexes increase index maintenance overhead.
  • Queries that do not use the leading column may not benefit efficiently.

8. Composite Index vs Single-Column Index

Single-Column Index Composite Index
Created on one column. Created on two or more columns.
Useful for queries filtering on one column. Useful for queries filtering on multiple columns.
Has one indexed column. Has multiple indexed columns.
Example: EMP_NAME. Example: DEPARTMENT_ID, SALARY.
Column order is not applicable. Column order is important.

9. Important Oracle Interview Questions

Q1. What Is a Composite Index in Oracle?

Answer: An index created on two or more columns of a table is called a Composite Index. It helps improve query performance when multiple indexed columns are used in SQL conditions.

Q2. Does Column Order Matter in a Composite Index?

Answer: Yes. Column order matters because Oracle generally uses the leading column to access the index efficiently.

Q3. Can We Create a Composite Index on Three Columns?

Answer: Yes. Oracle allows us to create an index on multiple columns.

Example:

CREATE INDEX idx_emp_details
ON employee(department_id, salary, emp_name);

Q4. Can a Composite Index Be Unique or Non-Unique?

Answer: Yes. Oracle supports both Composite Unique Indexes and Composite Non-Unique Indexes.

Q5. Can Oracle Use a Composite Index If We Specify Only the Second Column?

Answer: Generally, Oracle cannot efficiently use it for a normal leading-column index lookup. However, Oracle may choose an Index Skip Scan depending on the data distribution, statistics and execution plan.

Q6. How Many Columns Can We Include in a Composite Index?

Answer: Oracle supports indexes on multiple columns, subject to database-version-specific limits and index key size restrictions.

Q7. Can We Create Multiple Composite Indexes on a Single Table?

Answer: Yes. Oracle allows multiple Composite Indexes on a single table. However, they should be created based on actual SQL workload requirements.

Q8. What Is the Leading Column in a Composite Index?

Answer: The first column specified when creating a Composite Index is called the Leading Column.

10. Easy Way to Remember

Single-Column Index

An index created on only one column.

Example:

CREATE INDEX idx_emp_name
ON employee(emp_name);

Composite Index

An index created on two or more columns.

Example:

CREATE INDEX idx_dept_salary
ON employee(department_id, salary);
Interview One-Line Answer:

A Composite Index in Oracle is an index created on two or more columns of a table to improve query performance when multiple columns are used in WHERE conditions, JOIN conditions or sorting operations.

Conclusion

Composite Indexes are important Oracle Database objects that help improve SQL query performance when multiple columns are frequently used in queries.

We can create Composite Indexes on two or more columns and use them for equality conditions, range conditions, joins and sorting operations.

Always remember that column order matters in a Composite Index. Choosing the correct leading column is important for efficient index access.

Understanding Composite Indexes is essential for Oracle SQL Developers, Oracle DBAs and database performance tuning.

Keep Learning Oracle SQL and Database Performance Tuning!

No comments:

Post a Comment