Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase «««123

primary key discussion Expand / Collapse
Author
Message
Posted Friday, August 29, 2014 11:34 AM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Today @ 3:33 PM
Points: 8,826, Visits: 9,385
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
Post #1608709
Posted Friday, August 29, 2014 11:48 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 3:29 PM
Points: 2,079, Visits: 3,123
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)
"And in the evening, After the fire and the light /
One thing is certain: Nothing can hold back the night /
Time is relentless, And as the past disappears /
We're on the verge of all things new, We are two thousand years" : the inimitable Mr. Billy Joel
Post #1608712
Posted Friday, August 29, 2014 1:03 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 3:46 PM
Points: 23,396, Visits: 32,227
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.



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)
Post #1608725
Posted Friday, August 29, 2014 1:11 PM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 3:29 PM
Points: 2,079, Visits: 3,123
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)
"And in the evening, After the fire and the light /
One thing is certain: Nothing can hold back the night /
Time is relentless, And as the past disappears /
We're on the verge of all things new, We are two thousand years" : the inimitable Mr. Billy Joel
Post #1608726
Posted Friday, August 29, 2014 1:15 PM
SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Today @ 2:54 PM
Points: 1,956, Visits: 5,090
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.
Post #1608729
Posted Friday, August 29, 2014 1:17 PM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Today @ 6:38 AM
Points: 1,945, Visits: 3,001
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.

I did a job for a coupon processing company many years ago who made a scanner + shredder for use in a POS system. They cared about the issuer and the subcategories (P&G, soaps, Ivory brand, 125 gm bar).

After that, each issuer had an internal category system. If I had to do a general category system, I would probably use an abbreviation code.



Books in Celko Series for Morgan-Kaufmann Publishing
Analytics and OLAP in SQL
Data and Databases: Concepts in Practice
Data, Measurements and Standards in SQL
SQL for Smarties
SQL Programming Style
SQL Puzzles and Answers
Thinking in Sets
Trees and Hierarchies in SQL
Post #1608730
Posted Friday, August 29, 2014 1:23 PM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 3:29 PM
Points: 2,079, Visits: 3,123
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.


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)
"And in the evening, After the fire and the light /
One thing is certain: Nothing can hold back the night /
Time is relentless, And as the past disappears /
We're on the verge of all things new, We are two thousand years" : the inimitable Mr. Billy Joel
Post #1608732
Posted Friday, August 29, 2014 1:42 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 3:46 PM
Points: 23,396, Visits: 32,227
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.




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)
Post #1608739
Posted Friday, August 29, 2014 5:34 PM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Today @ 3:33 PM
Points: 8,826, Visits: 9,385
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
Post #1608781
« Prev Topic | Next Topic »

Add to briefcase «««123

Permissions Expand / Collapse