I’m creating an index in sql server 2005 and the discussion with a coworker is if it makes a difference between index key columns being id and date vs date then id.
Is there a fundamental difference in the way the index would be created in either scenario?
Would it make a difference in other versions of SQL server?
Thanks
Yes, definitely. Does anyone ever query the table for JUST date or JUST id? An index of date,id can be used to look up just date, but not just id, and vice-versa
Using date,id:
Using id,date:
If your WHERE clause or a JOIN in your query is using both date and id, then either index is fine. But you can see that if you’re doing a lookup just by date, the first index is useful for that, but the second one is totally random.
In a more general sense, an index on A, B, C, D is going to be useful for queries on A,B,C,D, OR A,B,C OR A,B OR just A.