A composite index is an index on two or more columns in a table. It helps speed up queries that filter or sort by those columns together.
Click to reveal answer
intermediate
Why use a composite index instead of multiple single-column indexes?
A composite index is more efficient for queries that use multiple columns together because it stores combined values, reducing the need to merge separate indexes.
Click to reveal answer
intermediate
How does the order of columns in a composite index affect query performance?
The order matters because MySQL uses the leftmost prefix of the index. Queries filtering on the first columns in order can use the index efficiently, but filtering only on later columns may not use it.
Click to reveal answer
beginner
Example: CREATE INDEX idx_name_age ON users(name, age); What does this do?
It creates a composite index on the 'name' and 'age' columns of the 'users' table. Queries filtering by name or by both name and age can use this index.
Click to reveal answer
intermediate
Can a composite index speed up queries filtering only by the second column?
No, because MySQL uses the leftmost prefix rule. The index can speed up queries filtering by the first column or the first and second columns together, but not by the second column alone.
Click to reveal answer
What does a composite index in MySQL consist of?
AAn index on multiple columns combined
BAn index on a single column only
CA temporary index created during queries
DAn index that stores only unique values
✗ Incorrect
A composite index combines two or more columns into one index to speed up queries using those columns together.
Which query can use the composite index on (name, age)?
AWHERE age = 30
BWHERE name = 'Alice' AND age = 30
CWHERE city = 'NYC'
DWHERE age > 25
✗ Incorrect
The composite index on (name, age) can be used when filtering by name alone or by both name and age, but not by age alone.
Why is the order of columns important in a composite index?
ABecause the order does not affect performance
BBecause the last column is always ignored
CBecause columns are sorted alphabetically in the index
DBecause MySQL uses the leftmost columns first in the index
✗ Incorrect
MySQL uses the leftmost prefix of the composite index, so queries filtering on those columns can use the index efficiently.
What happens if you create separate indexes on 'name' and 'age' instead of a composite index?
AQueries filtering by both columns may be slower than with a composite index
BQueries filtering by both columns will always be faster
CMySQL merges the indexes automatically with no difference
DIndexes on single columns cannot be created
✗ Incorrect
Separate indexes may require MySQL to merge results, which can be slower than using a composite index for queries filtering on both columns.
Which of these is a valid reason to use a composite index?
ATo make all queries slower
BTo reduce the size of the database
CTo speed up queries filtering on multiple columns together
DTo avoid creating any indexes on the table
✗ Incorrect
Composite indexes improve performance for queries that filter or sort by multiple columns together.
Explain what a composite index is and why the order of columns matters.
Think about how MySQL uses the first columns in the index for filtering.
You got /3 concepts.
Describe a scenario where a composite index improves query performance compared to single-column indexes.
Imagine searching a phone book by last name and first name together.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of a composite index in MySQL?
easy
A. To create a backup of the database automatically
B. To speed up queries that filter or sort by multiple columns together
C. To store data in multiple tables simultaneously
D. To encrypt data in multiple columns
Solution
Step 1: Understand what an index does
An index helps MySQL find data faster by organizing it for quick lookup.
Step 2: Understand composite index purpose
A composite index uses multiple columns together to speed up queries that filter or sort by those columns combined.
Final Answer:
To speed up queries that filter or sort by multiple columns together -> Option B
Quick Check:
Composite index = speed up multi-column queries [OK]
Hint: Composite indexes speed up multi-column filters and sorts [OK]
Common Mistakes:
Thinking composite indexes store data
Confusing indexes with backups
Assuming encryption is related to indexes
2. Which of the following is the correct syntax to create a composite index on columns first_name and last_name in a table users?
easy
A. CREATE INDEX idx_name ON users (first_name, last_name);
B. CREATE INDEX idx_name ON users first_name, last_name;
C. CREATE INDEX idx_name (first_name, last_name) ON users;
D. CREATE INDEX idx_name ON users WHERE first_name, last_name;
Solution
Step 1: Recall correct CREATE INDEX syntax
The syntax is: CREATE INDEX index_name ON table_name (column1, column2);
Step 2: Match syntax with options
CREATE INDEX idx_name ON users (first_name, last_name); matches the correct syntax with parentheses around columns and ON keyword before table name.
Final Answer:
CREATE INDEX idx_name ON users (first_name, last_name); -> Option A
Quick Check:
CREATE INDEX ... ON table (columns) [OK]
Hint: Use parentheses around columns after ON table_name [OK]
Common Mistakes:
Omitting parentheses around columns
Placing columns before ON keyword
Using WHERE instead of column list
3. Given a composite index on (city, age) in a table people, which query will use the index efficiently?
medium
A. SELECT * FROM people WHERE city = 'Paris' AND age = 30;
B. SELECT * FROM people WHERE age = 30;
C. SELECT * FROM people WHERE age BETWEEN 20 AND 40;
D. SELECT * FROM people WHERE city LIKE '%Paris%' ORDER BY age DESC;
Solution
Step 1: Understand composite index column order
The index is on (city, age), so queries filtering city first then age use it best.
Step 2: Analyze queries for index usage
SELECT * FROM people WHERE city LIKE '%Paris%' ORDER BY age DESC;: LIKE '%Paris%' prevents index use due to leading wildcard. SELECT * FROM people WHERE age = 30;: filters only age (second column), skips prefix. SELECT * FROM people WHERE age BETWEEN 20 AND 40;: range on age only, cannot use prefix. SELECT * FROM people WHERE city = 'Paris' AND age = 30;: city then age equality matches prefix perfectly.
Final Answer:
SELECT * FROM people WHERE city = 'Paris' AND age = 30; -> Option A
Quick Check:
Filter starts with first index column = SELECT * FROM people WHERE city = 'Paris' AND age = 30; [OK]
Hint: Filter starting with first index column uses composite index best [OK]
Common Mistakes:
Assuming any column filter uses composite index fully
Ignoring column order in composite index
Thinking ORDER BY always uses composite index
4. You created a composite index on (department, salary) but your query SELECT * FROM employees WHERE salary > 50000; is slow. What is the likely problem?
medium
A. The table has no data
B. The index syntax was incorrect
C. Composite indexes cannot be used with numeric columns
D. The query does not filter on the first column of the composite index
Solution
Step 1: Recall composite index usage rules
Composite indexes work best when queries filter starting from the first indexed column.
Step 2: Analyze the query and index
The index is on (department, salary), but query filters only salary (second column), so index is not used efficiently, causing slowness.
Final Answer:
The query does not filter on the first column of the composite index -> Option D
Quick Check:
Filter must start with first index column [OK]
Hint: Filter must include first index column to use composite index [OK]
Common Mistakes:
Assuming any column filter uses composite index
Blaming syntax without checking query
Thinking numeric columns can't be indexed
5. You have a table orders with columns customer_id, order_date, and status. You often run this query: SELECT * FROM orders WHERE customer_id = 123 AND status = 'shipped' ORDER BY order_date DESC; Which composite index will optimize this query best?
hard
A. CREATE INDEX idx_order_date_customer_status ON orders (order_date, customer_id, status);
B. CREATE INDEX idx_status_customer_date ON orders (status, customer_id, order_date);
C. CREATE INDEX idx_customer_status_date ON orders (customer_id, status, order_date);
D. CREATE INDEX idx_customer_order_date ON orders (customer_id, order_date);
Solution
Step 1: Identify query filter and order columns
The query filters by customer_id and status, then orders by order_date descending.
Step 2: Choose index column order for best performance
Index should start with columns used in WHERE filters in order: customer_id, then status, then order_date for ORDER BY.
Step 3: Match index options
CREATE INDEX idx_customer_status_date ON orders (customer_id, status, order_date); matches this order exactly, so it optimizes filtering and ordering best.
Final Answer:
CREATE INDEX idx_customer_status_date ON orders (customer_id, status, order_date); -> Option C
Quick Check:
Index columns match WHERE then ORDER BY order [OK]
Hint: Index columns order: filter columns first, then order column [OK]
Common Mistakes:
Putting ORDER BY column before filter columns
Ignoring filter columns order
Creating index missing important filter column
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.