MySQL Composite Indexes Explained
Quick Answer
A MySQL composite index is an index on multiple columns in a table. It improves query performance when filtering or sorting by those columns together. Composite indexes are most effective when queries use the leftmost prefix of the indexed columns.
Learning Objectives
- Understand what a composite index is in MySQL.
- Learn how composite indexes improve query performance.
- Identify best practices for creating and using composite indexes.
Introduction to MySQL Composite Indexes
Indexes in MySQL help speed up data retrieval by allowing the database to find rows faster.
A composite index is an index on two or more columns of a table, designed to optimize queries that filter or sort on multiple columns.
Understanding how composite indexes work is essential for writing efficient SQL queries and improving database performance.
An index on multiple columns is only as good as the queries that use its leftmost columns.
What is a Composite Index?
A composite index in MySQL is an index that includes more than one column. It stores the values of these columns in a sorted order to speed up searches involving those columns.
Unlike single-column indexes, composite indexes allow queries to filter or sort on multiple columns efficiently.
- Indexes multiple columns together in a specific order.
- Improves performance for queries using those columns combined.
- Supports leftmost prefix matching for query optimization.
How Composite Indexes Work
MySQL stores composite indexes in a sorted structure based on the order of columns defined in the index.
When a query filters on the leftmost columns of the composite index, MySQL can quickly locate matching rows without scanning the entire table.
- The order of columns in the index matters.
- Queries using the leftmost columns benefit the most.
- Queries skipping the first column cannot fully utilize the composite index.
Example of Leftmost Prefix Usage
Consider a composite index on columns (last_name, first_name).
A query filtering on last_name alone or last_name and first_name can use this index efficiently.
However, a query filtering only on first_name cannot use this composite index effectively.
Creating Composite Indexes in MySQL
You can create a composite index using the CREATE INDEX statement or when creating a table.
Specify the columns in the order that matches your most common query patterns.
Syntax Example
To create a composite index on columns last_name and first_name in a table called employees, use:
Practical Example
This example creates a composite index on last_name and first_name. The SELECT query filters on both columns, allowing MySQL to use the index for faster lookup.
Examples
CREATE INDEX idx_name ON employees (last_name, first_name);
-- Query that uses the composite index
SELECT * FROM employees WHERE last_name = 'Smith' AND first_name = 'John';This example creates a composite index on last_name and first_name. The SELECT query filters on both columns, allowing MySQL to use the index for faster lookup.
Best Practices
- Order columns in the composite index based on the most common query filters starting from the left.
- Avoid creating unnecessary composite indexes to reduce overhead on writes.
- Use EXPLAIN to verify if your queries use the composite index effectively.
Common Mistakes
- Creating composite indexes without considering query patterns and column order.
- Assuming composite indexes speed up queries filtering on any column in the index regardless of order.
- Over-indexing tables with many composite indexes, causing slower inserts and updates.
Hands-on Exercise
Create and Test a Composite Index
Create a composite index on two columns of a sample table and write queries that use the index effectively and queries that do not.
Expected output: Queries filtering on the leftmost columns should show improved performance using EXPLAIN.
Hint: Focus on filtering using the leftmost columns of the index.
Interview Questions
What is a composite index in MySQL?
InterviewA composite index is an index on multiple columns in a table, used to optimize queries filtering or sorting on those columns together.
Why does the order of columns matter in a composite index?
InterviewBecause MySQL uses the leftmost prefix of the composite index for query optimization, so queries must filter on the leading columns to benefit.
Can a composite index be used if a query filters only on the second column?
InterviewNo, if the query does not filter on the leftmost column(s), the composite index cannot be fully utilized.
MCQ Quiz
1. What is the best first step when learning Composite Index?
A. Understand the purpose and basic idea
B. Skip directly to advanced implementation
C. Ignore examples and practice
D. Memorize terms without context
Correct answer: A
Starting with the purpose and basic idea makes later examples and practice easier to understand.
2. Which activity helps reinforce Composite Index?
A. Reading once without practice
B. Building or writing a small practical example
C. Avoiding review questions
D. Skipping the summary
Correct answer: B
A small practical example helps connect the topic to real usage.
3. Which statement is most accurate about this topic?
A. A MySQL composite index is an index on multiple columns in a table.
B. Composite Index never needs examples
C. Composite Index is unrelated to practical work
D. Composite Index should be learned without checking results
Correct answer: A
The correct option is based on the available topic explanation.
Key Takeaways
- Composite indexes index multiple columns in a defined order.
- Queries benefit most when filtering uses the leftmost columns of the composite index.
- Proper use of composite indexes can significantly speed up multi-column searches.
- A MySQL composite index is an index on multiple columns in a table.
- It improves query performance when filtering or sorting by those columns together.
Summary
Composite indexes in MySQL index multiple columns in a specific order to optimize multi-column queries.
The order of columns is critical because MySQL uses the leftmost prefix for query optimization.
Properly designed composite indexes can significantly improve query performance, but misuse can lead to inefficiencies.
Frequently Asked Questions
What is the difference between a single-column index and a composite index?
A single-column index indexes one column, while a composite index indexes multiple columns together in a defined order.
Can a composite index speed up queries that filter on only one column?
Yes, but only if the filtered column is the leftmost column in the composite index.
How do I know if my query uses a composite index?
Use the EXPLAIN statement before your query to see if MySQL uses the composite index for execution.





