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)
 Column Order for Primary Key

Author  Topic 

asqldeveloper
Starting Member

17 Posts

Posted - 2013-08-20 : 21:39:18
I understand that the order of columns doesnt change performance but if my primary key is made of two or more columns and its not the first columns, but spreadout in the table, would that affect index seeks or scans or affect performance. Example below.

Lets say my primarykey is OfficeName, OfficeCounty, OfficeState
and my table script is

CREATE TABLE OFFICE
(
OfficeName,
OfficeIsActive,
OfficeIsWalkableDistance,
OfficeCanBeReachedByPhone,
OfficeSupportsPrinter,
OfficeCounty,
OfficeManagerFirstName,
OfficeManagerLastName,
OfficeState
).

As you can see my primary key is spread out in the table, does it matter?

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-08-21 : 00:33:27
the order of columns in the index do matter

see

http://weblogs.sqlteam.com/joew/archive/2008/02/13/60510.aspx

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

Bustaz Kool
Master Smack Fu Yak Hacker

1834 Posts

Posted - 2013-08-22 : 11:22:31
The order of the columns within the index does matter, as has been pointed out already. The order of those columns within the table does not (I'm not sure if that was your question or not). Moving OfficeCounty and/or OfficeState to be "earlier" in the table would have no effect on your index performance.

=================================================
The cure for anything is salt water -- sweat, tears, or the sea. -Isak Dinesen
Go to Top of Page

asqldeveloper
Starting Member

17 Posts

Posted - 2013-08-23 : 13:37:45
Kool

You got my question right and Thanks for answering it. Thats what I thought, but wanted to make sure. I do know that the order of index matters. Thanks.

I designed in Erwin, but I guess there is a check box which you should uncheck (and its set by default) which makes it create the table in random colum order. If you uncheck it, it would create in the exact column order you designed in Erwin.

Why on earth that it would be set to scramble by default is beyond me.
Go to Top of Page
   

- Advertisement -