Solved

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

Posted on 2007-03-29
6
355 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how the fundamental information of how to create a table.

777 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