đ§Š A Composite Index is an index created on two or more columns of a table. Instead of indexing a single column, it stores combinations of multiple column values, allowing the database to efficiently execute queries that filter, sort, or join using those columns together.
đ What is a Composite Index?
A composite index (also called a multi-column index or compound index) combines multiple columns into a single index. It is particularly useful when queries frequently use the same set of columns in their search conditions.
Information
đ¯ Why Use a Composite Index?
Composite indexes improve performance when multiple columns are commonly used together in SQL queries.
- đ Speed up multi-column searches.
- đ Improve filtering with multiple WHERE conditions.
- đ Optimize sorting using ORDER BY.
- đ Improve table joins involving multiple columns.
- đ Reduce the need for multiple single-column indexes.
đ Sample Table
| StudentID | Name | Department | City |
|---|---|---|---|
| 101 | Alice | Computer Science | Chennai |
| 102 | Bob | Computer Science | Madurai |
| 103 | Charlie | Mathematics | Chennai |
đ Basic Syntax
Composite Index Syntax
CREATE INDEX index_name
ON table_name (column1, column2, ...);đĄ Create a Composite Index
Create an index using both the Department and Name columns.
Create a Composite Index
CREATE INDEX idx_department_name
ON Students (Department, Name);This index stores combinations of Department and Name, making related searches more efficient.
đ Query That Benefits from the Index
Search Using Both Indexed Columns
SELECT *
FROM Students
WHERE Department = 'Computer Science'
AND Name = 'Alice';Since the query filters on both indexed columns, the database can efficiently use the composite index.
đ Understanding Column Order
Suppose the following composite index exists:
Composite Index
CREATE INDEX idx_department_name
ON Students (Department, Name);| Query | Can Efficiently Use the Index? |
|---|---|
| WHERE Department = 'Computer Science' | â Yes |
| WHERE Department = 'Computer Science' AND Name = 'Alice' | â Yes |
| WHERE Name = 'Alice' | â Usually not efficiently by itself. |
Important
đ Composite Index for Sorting
Composite indexes can also improve sorting operations when the ORDER BY clause matches the indexed column order.
Filtering and Sorting
SELECT *
FROM Students
WHERE Department = 'Computer Science'
ORDER BY Name;Because the index is organized by Department followed by Name, the database may avoid an additional sorting step.
đ Single-Column Index vs Composite Index
| Feature | Single-Column Index | Composite Index |
|---|---|---|
| Indexed Columns | One | Two or more |
| Best For | Single-column searches. | Multi-column searches. |
| Sorting Optimization | Limited. | Excellent when column order matches. |
| Storage Usage | Lower. | Higher. |
đŧ Real-World Example
An online shopping platform frequently searches products by Category and Brand. Creating a composite index on these columns significantly improves search performance.
Products Composite Index
CREATE INDEX idx_category_brand
ON Products (Category, Brand);Queries such as the following can benefit from the index:
Product Search
SELECT *
FROM Products
WHERE Category = 'Laptop'
AND Brand = 'Dell';đī¸ Database Compatibility
| Database System | Composite Index Support |
|---|---|
| MySQL | â Fully supported. |
| PostgreSQL | â Fully supported. |
| SQL Server | â Fully supported. |
| Oracle | â Fully supported. |
| SQLite | â Supports multi-column indexes. |
â ī¸ Common Mistakes
- â Choosing an inefficient column order.
- â Creating composite indexes for columns rarely queried together.
- â Creating multiple overlapping indexes unnecessarily.
- â Ignoring additional storage and maintenance costs.
Warning
â ī¸ Best Practices
Best Practice
đ Key Points to Remember
- đ A composite index contains two or more columns.
- đ Column order is critical for index effectiveness.
- đ Composite indexes improve multi-column filtering and sorting.
- đ They can reduce the need for several separate indexes.
- đ They consume additional storage and require maintenance.
- đ Composite indexes are supported by all major relational database systems.