Suppose I have following table
UserID
(Identity) PKUserName
- unique non nullUserEmail
- unique non nullWhat is recommended for the best performance?
UserName
and UserEmail
separatelyOR
Please do share you thoughts why one is preferable over other.
Another important point to consider is this: a compound index (made up of multiple columns) will only be used if the n left-most columns are being referenced (e.g. in a WHERE
clause).
So if you have a single compound index on
(UserID, UserName, UserEmail)
then this index might be used in the following scenarios:
UserID
alone (using just the 1 left-most column - UserID
)UserID
and UserName
(using the 2 left-most columns)But this single compound index will never be able to be used for searches on
UserName
- it's the second column in the index and thus this index cannot ever be usedUserEmail
- it's the third column in the index and thus this index cannot ever be usedJust remember this - just because a column is part in an index doesn't necessarily mean that searching on that single column alone will be supported and sped up by that index!
So if your usage patterns and your application really need to search on UserName
and/or UserEmail
alone (without providing other search values), then you must create separate indices on these columns - just a single compound one will not have any benefit at all.