Shawn
Shawn

Reputation: 34269

mysql index usage question

I have a simple query that looks like

select id, name from mytable where age=28 order by name

the EXPLAIN output confuses me, I got

possible_keys : age
key : name

Does that mean mysql finally decided to use the index 'name' to fetch all the rows? Is the age index used or not? How many indexes are used in this query? If it's the name index that mysql finnally chooses to use, what happened to the 'age' index?

Upvotes: 2

Views: 213

Answers (1)

GolezTrol
GolezTrol

Reputation: 116170

Indeed, name is used and age is not. Best solution is to have a combined key on both age and name (in that order). That way, queries can make use of this index when sorting and filtering on both columns.

Upvotes: 1

Related Questions