Lets say I have following query:
SELECT * FROM table WHERE id = 1 ORDER BY name
Is it better to add multi-column index (id and name) or separate index for name?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
That depends on the cardinality of
id. If it has a high cardinality (meaning, few rows share the same value ofid) an index onidis a good idea. The database will do a seek foridand look the few matching rows up in the base table.If half the table has the same
id, the query will result in an index scan. Note that since you’re using*, a multi-column index would not be used unless it contains all columns in the table. So this can be made faster only with an index containing all columns that is sorted on(name).At the end of the day, it’s often best not to create an index at all, until you encounter specific performance problems. The specifics will allow you to test & measure whether your proposed index will work. That’s almost always better than trying to guess the right indexes.