SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 SQL Server Administration (2008)
 Does order of columns in an index matter?
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

gbritton
Aged Yak Warrior

604 Posts

Posted - 06/20/2013 :  10:22:05  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

3578 Posts

Posted - 06/20/2013 :  10:56:59  Show Profile  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000