Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


primary key discussion


primary key discussion

Author
Message
TomThomson
TomThomson
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10696 Visits: 11991
If queries which scan a range of primary key are used very often, you may want to cluster on the primary key; where the primary key is multi-column, that includes the case where queries that pick a single value of some of the columns are common (and which such queries are common may tell you what order the columns should have within the primary key). If queries that come from outside the database normally use the primary key, you should not use a surrogate as primary key but that doesn't necessarily mean you don't want to cluster on the surrogate instead of on the primary key - or indeed want to cluster on something else altogether. It is somethimes reasonable to write joins using the surrogate but still specify the natural key as the primary key and cluster on something that is neither the primary key nor the surrogate. You may find that where a table is not a gateway to the database the besy choice for primary key something which is a surrogate derived for use in joins in a parent table which uses the natural primary key - and you may find this even when the parent table clusters on the natural key if for example the child table has far fewer rows than the parent.

It all depends on what the query workload and the row counts look like.

I think Grant's advice in his comment above is spot on.

Tom

ScottPletcher
ScottPletcher
Hall of Fame
Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)

Group: General Forum Members
Points: 3937 Visits: 6665
The page split / fragmentation concern is often overblown. Remember, one INSERT, but you may read the row 1000, 10K, 100K+(?) or more times. Particularly given the availability of partitions, reorganizations, online rebuilds, etc., some fragmentation can be dealt with far better than using the wrong clus key just to reduce frag on the single INSERT of each row while drastically harming the performance of the vast majority of future SELECTs.

SQL DBA,SQL Server MVP('07, '08, '09)

Prosecutor James Blackburn, in closing argument in the "Fatal Vision" murders trial: "If in the future, you should cry a tear, cry one for them [the murder victims]. If in the future, you should say a prayer, say one for them. And if in the future, you should light a candle, light one for them."
Lynn Pettis
Lynn Pettis
SSC-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24167 Visits: 37930
ScottPletcher (8/29/2014)
The page split / fragmentation concern is often overblown. Remember, one INSERT, but you may read the row 1000, 10K, 100K+(?) or more times. Particularly given the availability of partitions, reorganizations, online rebuilds, etc., some fragmentation can be dealt with far better than using the wrong clus key just to reduce frag on the single INSERT of each row while drastically harming the performance of the vast majority of future SELECTs.


Until you find yourself in that position. I haven't but I have talked with someone who was in that exact environment and defragging a highly fragmented clustered index would basically shutdown the mirroring to the DR site which is over a slow WAN connection.

You have to work with what you have and make the systems work under less than optimum conditions.

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
ScottPletcher
ScottPletcher
Hall of Fame
Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)

Group: General Forum Members
Points: 3937 Visits: 6665
Lynn Pettis (8/29/2014)
ScottPletcher (8/29/2014)
The page split / fragmentation concern is often overblown. Remember, one INSERT, but you may read the row 1000, 10K, 100K+(?) or more times. Particularly given the availability of partitions, reorganizations, online rebuilds, etc., some fragmentation can be dealt with far better than using the wrong clus key just to reduce frag on the single INSERT of each row while drastically harming the performance of the vast majority of future SELECTs.


Until you find yourself in that position. I haven't but I have talked with someone who was in that exact environment and defragging a highly fragmented clustered index would basically shutdown the mirroring to the DR site which is over a slow WAN connection.

You have to work with what you have and make the systems work under less than optimum conditions.


How did it get "highly" fragmented? For a very large table, that would take a very large number of "bad" (mid-page) page splits, which would not occur with a reasonable clustering key, even if it wasn't universally ascending.

SQL DBA,SQL Server MVP('07, '08, '09)

Prosecutor James Blackburn, in closing argument in the "Fatal Vision" murders trial: "If in the future, you should cry a tear, cry one for them [the murder victims]. If in the future, you should say a prayer, say one for them. And if in the future, you should light a candle, light one for them."
Eirikur Eiriksson
Eirikur Eiriksson
SSCertifiable
SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)

Group: General Forum Members
Points: 6693 Visits: 17683
Thank you Scott, Tom and Lynn, this was the discussion I wanted to get going in this context. The subject is far to dependent on the environment and the nature of the dominant activities for one to generalise.
Cool
ScottPletcher
ScottPletcher
Hall of Fame
Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)

Group: General Forum Members
Points: 3937 Visits: 6665
Eirikur Eiriksson (8/29/2014)
Thank you Scott, Tom and Lynn, this was the discussion I wanted to get going in this context. The subject is far to dependent on the environment and the nature of the dominant activities for one to generalise.
Cool


Exactly correct! Don't let some nursery-rhyme like saying -- "narrow, unique, ever-ascending" -- bulldoze you into a cluster key selection: do some real analysis, and decide on the best key for your specific situation. The clus key is the single most important factor for performance (in most cases), so don't assume one simple general rule fits your exact situation.

SQL DBA,SQL Server MVP('07, '08, '09)

Prosecutor James Blackburn, in closing argument in the "Fatal Vision" murders trial: "If in the future, you should cry a tear, cry one for them [the murder victims]. If in the future, you should say a prayer, say one for them. And if in the future, you should light a candle, light one for them."
Lynn Pettis
Lynn Pettis
SSC-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24167 Visits: 37930
ScottPletcher (8/29/2014)
Lynn Pettis (8/29/2014)
ScottPletcher (8/29/2014)
The page split / fragmentation concern is often overblown. Remember, one INSERT, but you may read the row 1000, 10K, 100K+(?) or more times. Particularly given the availability of partitions, reorganizations, online rebuilds, etc., some fragmentation can be dealt with far better than using the wrong clus key just to reduce frag on the single INSERT of each row while drastically harming the performance of the vast majority of future SELECTs.


Until you find yourself in that position. I haven't but I have talked with someone who was in that exact environment and defragging a highly fragmented clustered index would basically shutdown the mirroring to the DR site which is over a slow WAN connection.

You have to work with what you have and make the systems work under less than optimum conditions.


How did it get "highly" fragmented? For a very large table, that would take a very large number of "bad" (mid-page) page splits, which would not occur with a reasonable clustering key, even if it wasn't universally ascending.


Never asked for the details. What made sense as a clustered index for data access would fragment the index rapidly affecting system performance. Defragging the index would essentially shutdown the mirroring to the DR site.

The choice of a clustering index is more than data access it is also dependent on the environment. In this case a very narrow ever increasing clustered index was the best choice as it kept the index from fragmenting.

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
TomThomson
TomThomson
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10696 Visits: 11991
CELKO (8/29/2014)
How about something like Coupon types? What would you use there? I am certainly not going to use the description of the type (PercentageDiscount, FreeShipping, BOGO, etc).


A type is an attribute, measured on an enumeration scale. It is not an identifier, so it cannot be key.

It seems to me reasonable to interpret Sean as referring to a column whose domain is a Coupon Descriptions (or Coupon Type Names if you would rather call them that) and it seems to me to require a considerable degree of unreasonableness to interpret him differently.
I know that the SQL standard doesn't include such domains, but it is easy enough to define such a domain using a varchar type and a check constraint, or alternatively with an auxiliary table enumerating the values and having an interger (or tinyint, smallint, or bigint) surrogate key with the domain being represented in other tables by an integer column with a foreign key reference to the auxiliary table.

Tom

Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search