1

Let's say I have a table Person:

ID 
Name
Surname
DateOfBirth

And I have this composite index: (Name, Surname)

Do I have to create an implicit index for Name? e.g For a where clause like that:

SELECT * 
FROM Person 
WHERE Name=?

Or it's not needed as the previous index will cover it?

4
  • 3
    No, you do not need another index. Your composite index covers it. Commented Mar 13, 2016 at 18:33
  • Gordon is right. But if you want proof, check the execution plan. Commented Mar 13, 2016 at 18:37
  • Thanks for the clarification! Commented Mar 13, 2016 at 18:38
  • 3
    Note that it will not work for Surname Commented Mar 13, 2016 at 19:24

1 Answer 1

2

You don't need a separate index to query the NAME column, if the NAME column is the first column in your composite index. This is because your index is ordered by NAME ASC, SURNAME ASC, so if you want to look up a NAME value, your database can scan only the relevant parts of the index.

However, your index will be pretty useless if you filter by SURNAME only, in case of which the whole index would need to be traversed anyway.

This website explains this nicely: http://use-the-index-luke.com/sql/where-clause/the-equals-operator/concatenated-keys

Sign up to request clarification or add additional context in comments.

1 Comment

His book is highly recommended (SQL Performance Explained, Markus Winand)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.