0
0
MySQLquery~5 mins

Composite indexes in MySQL - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a composite index in MySQL?
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
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
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
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
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
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.