Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 SQL Server Administration (2008)
 Does order of columns in an index matter?

Author  Topic 

gbritton
Master Smack Fu Yak Hacker

2780 Posts

Posted - 2013-06-20 : 10:22:05
Say I have a table with three columns: A, B, C

Say that I have an index on columns (A, B)

If I have a new query that does a join on column B, would it be a good idea to make a new index on just column B (or on columns (B,A))?

In other words, does the order of the columns in the index matter, or is SQL Server smart enough to handle my new query efficiently using the first index?

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-06-20 : 10:56:59
quote:
Originally posted by gbritton

Say I have a table with three columns: A, B, C

Say that I have an index on columns (A, B)

If I have a new query that does a join on column B, would it be a good idea to make a new index on just column B (or on columns (B,A))?

In other words, does the order of the columns in the index matter, or is SQL Server smart enough to handle my new query efficiently using the first index?

The order of columns on an index does matter. Think of the index as being exactly like a phonebook where the "index" is (lastname, firstname). With that info, if you wanted to find everyone with the lastname Smith, you know exactly how to do it. But if you wanted to find everyone with the firstname Jane, you couldn't really use that index very efficiently.

SQL Server behaves in the same way - it will make use of the index, but only to find the firstname after it looks up each lastname.
Go to Top of Page
   

- Advertisement -