Solved

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

Posted on 2007-03-29
6
362 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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

Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

Question has a verified solution.

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

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

734 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