Solved

Multi column primary key, which column to use as leading edge?

Posted on 2007-03-29
6
353 Views
Last Modified: 2008-01-09
Given SQL SERVER 2000 and table:

CREATE TABLE Person
(
  CityID int not null,
  PersonID bigint not null,
  SomeOtherAttributes null
)
WHERE CityID is a FK to Cities table,  and PersonID is only unique within a city (Off topic but FYI - the PersonId is imported from another application which does not use globally unique identifiers).

The primary key options are:
 (CityId,PersonId)
or
 (PersonId,CityId)

My question is which primary key to specify and why/how to figure out which primary key to specify?

Thank you!

 
0
Comment
Question by:mjmarlow
6 Comments
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 18817264
cityid,personid maybe of use...

but what is the cardinality of the two columns (how many values )

what do you normally search with city ? or person ?

to a cetain extent it may not matter unless you are intending to cluster in the key as well...
 

 
0
 
LVL 29

Expert Comment

by:Nightman
ID: 18817275
PersonID, CityId

As a general principle, you should have your most selective column first.
0
 
LVL 10

Accepted Solution

by:
lahousden earned 500 total points
ID: 18817277
Both keys will serve well as PK.  However, there are some secondary considerations.  For example, if you see situations where selections are going to ask for all Person's in a particular City, or situations where reports on the Person table are to be ordered by CityID, then if you put CityId first in the Primary Key (like your first option) the query optimiser will be able to leverage the index that the Primary Key is based on, giving some performance benefits in these situations without requiring you to create an additional index on the table.
0
Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

 
LVL 7

Author Comment

by:mjmarlow
ID: 18817338
Some of my more complex queries  include a "CityId" qualifier in the join or where clause.   I noticed that  SQL Server index optimizer recommended then added an Index "CityId" on my Person table.  I wonder if I would spare the extra index overhead if i used CityId as the leading edge instead (of following the book of using most selective column).
0
 
LVL 29

Expert Comment

by:Nightman
ID: 18817376
That suggests that most of your queries are by CityId instead of PersonId. If that is the case, CityId would be the better option, as explained by lahousden
0
 
LVL 7

Author Comment

by:mjmarlow
ID: 18817820
Lowfatspread:

Cardinality:
Persons: 705151
Cities: 159

I often search for a subset  of (5-1000) persons who are isolated by City and presence in another table via a join on CityId and PersonId.

I do not intend on clustering the key, but from what i have read, clustering is done automatically if there are no other clustered indexes specified.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now