Solved

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

Posted on 2007-03-29
6
357 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

713 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