SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Candidate Key Or Composite Key


Candidate Key Or Composite Key

Author
Message
Gopal Rathore
Gopal Rathore
SSC Rookie
SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)

Group: General Forum Members
Points: 48 Visits: 8
Which is better to have either Numeric Candidate Key or Composite Key in the dependent table to make joins faster to execute say for example I have FormulaId field as primaryKey in Master table and FormulaId and RowId as Composite Key in Dependent table so this will be a better solution or I can put a Numeric Candidatekey in Primary and Dependent tables to allow joins faster?
goodguy
goodguy
SSCrazy
SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)

Group: General Forum Members
Points: 2112 Visits: 1037
As far as possible, I keep all my primary keys as numeric.
Use indexes to maintain order on String columns.


Regards,

goodguy

Experience is a bad teacher whose exams precede its lessons

Eugene Elutin
Eugene Elutin
SSC-Insane
SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)

Group: General Forum Members
Points: 23714 Visits: 5478
Gopal Rathore (6/25/2010)
Which is better to have either Numeric Candidate Key or Composite Key in the dependent table to make joins faster to execute say for example I have FormulaId field as primaryKey in Master table and FormulaId and RowId as Composite Key in Dependent table so this will be a better solution or I can put a Numeric Candidatekey in Primary and Dependent tables to allow joins faster?


If your only consideration is performance of joins, then most of the time 4 byte's INT or 8 byte's BIGINT key will perform better than a key of a large string (or other) datatype.
There are two main theories exist around what your primary key should be. Many people (myself as well) prefer to have surragate/artificial key as PK (Identity column is fine for this). However, there are many others (Joe Celko's followers) who would tell you to mainly use Natural Keys (and never use Identity and Bit datatypes as they are of too old age :-D). But, as well stated by Joe Celco himself "There is no such thing as a "universal, one-size-fits-all" key.". Here the link to his article about all sorts of keys :-D http://intelligent-enterprise.informationweek.com/showArticle.jhtml;jsessionid=THPULYB0UUJP5QE1GHRSKH4ATMY32JVN?articleID=201806814

Please Note: I am strongly dissagree with his stance on use of Identity columns and his defenition of lazy, non-RDBMS programmer (and many other of his propoganda) :-D. Saying that this article is worth reading anyway.

_____________________________________________
"The only true wisdom is in knowing you know nothing"
"O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
PaulB-TheOneAndOnly
PaulB-TheOneAndOnly
One Orange Chip
One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)

Group: General Forum Members
Points: 26559 Visits: 4639
Gopal Rathore (6/25/2010)
Which is better to have either Numeric Candidate Key or Composite Key in the dependent table to make joins faster to execute say for example I have FormulaId field as primaryKey in Master table and FormulaId and RowId as Composite Key in Dependent table so this will be a better solution or I can put a Numeric Candidatekey in Primary and Dependent tables to allow joins faster?


When you say "candidate key" you are meaning "surrogate key", aren't you?

My preference is to use "natural keys" whenever possible. This preference accepts a few exceptions like certain cases of dimensional modeling for an Oracle based Data Warehouse environment.

_____________________________________
Pablo (Paul) Berzukov

Author of Understanding Database Administration available at Amazon and other bookstores.

Disclaimer: Advice is provided to the best of my knowledge but no implicit or explicit warranties are provided. Since the advisor explicitly encourages testing any and all suggestions on a test non-production environment advisor should not held liable or responsible for any actions taken based on the given advice.
Ben Teraberry
Ben Teraberry
SSCarpal Tunnel
SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)

Group: General Forum Members
Points: 4831 Visits: 1199
Eugene touched on this, but let me expand a little ...

Join performance will be faster using as small of a numeric key as possible. Small is important because the index is stored on pages, so the smaller the datatype, the greater the number of records will be stored on a single page. Thus, SQL Server will have to look at fewer pages to get the data it needs. Numeric is important because SQL Server can make the fastest comparisons against numeric datatypes.

Now, all that being said, you're really only going to see a noticeable difference on higher end systems. My big suggestion would be to stay consistent with whatever you choose across the tables in your database.

└> bt


Forum Etiquette: How to post data/code on a forum to get the best help
Gopal Rathore
Gopal Rathore
SSC Rookie
SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)

Group: General Forum Members
Points: 48 Visits: 8
Let me little ellaborate my point.

It is suggested that joins on alphanumeric composite keys is much slower than joins on single numeric field. If I can put a numeric Document Number field on both Master and Detail tables and rather than working on composite key or said natural key on both the tables I can use single candidate key or said surrogate key for joins. For example I have Master table with FormulaId and RevNo, both alphanumeric fields in Master table as Composite Key and I have Detail table with FormulaId, RevNo and RowNum as CompositeKey. Now my question is can I add DocNum field which is integer type in Master Table and replace both formulaId and RevNo in Detail table with DocNum which is again of integer type. Now I can map DocNum in Master and Detail table in joins instead of mapping formulaId and RevNo in both Master and Detail tables. Also I will make DocNum in Master table as single Candidate Key instead of FormulaId and RevNo and DocNum and RowNum in Detail table as Composite Key. There will not be any FormulaId and RevNo fields in Detail table. Referencing will be made on DocNum on both Master and Detail table. Is this will be a good database design because it will make joins faster but again I will have to compromise with finding formulaId in Detail table as it will require another join to Master table of DocNum to find the corresponding formulaId?
PaulB-TheOneAndOnly
PaulB-TheOneAndOnly
One Orange Chip
One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)

Group: General Forum Members
Points: 26559 Visits: 4639
Gopal Rathore (6/30/2010)
Is this will be a good database design because it will make joins faster but again I will have to compromise with finding formulaId in Detail table as it will require another join to Master table of DocNum to find the corresponding formulaId?


Answer is embedded in the question :-)

To make joins work faster proposed design is adding I/O overhead - it defeats the purpose, isn't it?

_____________________________________
Pablo (Paul) Berzukov

Author of Understanding Database Administration available at Amazon and other bookstores.

Disclaimer: Advice is provided to the best of my knowledge but no implicit or explicit warranties are provided. Since the advisor explicitly encourages testing any and all suggestions on a test non-production environment advisor should not held liable or responsible for any actions taken based on the given advice.
Eugene Elutin
Eugene Elutin
SSC-Insane
SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)

Group: General Forum Members
Points: 23714 Visits: 5478
1. Using single numeric key (DocNo) will make join between your Master and Details table work faster!
2. Getting FormulaID when just retrieving details will be slower as it will require the join to the Master.
Now, what is more important for you? Join between two tables to work faster or getting the attribute/key of Master record from Details table without Master table lookup?
How often you need to get Details data bypassing Master table? Is it really required in your system?
As I said in my previous post, I do prefer use of numeric (int or bigint) PK. It will make joins (based on them) work faster and using joins between tables is quite common in RDBs :-D.
From another hand how often do you really need to bypass you "parent" table while getting data from "child" table? For example:
you have Product table with ProductName as one of its columns. Also, you have table ProductParts holding details of the product parts.
Would you need to display the ProductParts details without displaying which product they belongs to? Probably not. Does it mean you need to have ProductName stored in the ProductParts as well?
You can see the above as simple question of data normalisation...

_____________________________________________
"The only true wisdom is in knowing you know nothing"
"O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
Ben Teraberry
Ben Teraberry
SSCarpal Tunnel
SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)

Group: General Forum Members
Points: 4831 Visits: 1199
PaulB-TheOneAndOnly (6/30/2010)
Gopal Rathore (6/30/2010)
Is this will be a good database design because it will make joins faster but again I will have to compromise with finding formulaId in Detail table as it will require another join to Master table of DocNum to find the corresponding formulaId?


Answer is embedded in the question :-)

To make joins work faster proposed design is adding I/O overhead - it defeats the purpose, isn't it?

I don't believe this is correct unless you assume poorly developed indexes.

As with everything in SQL there are many situation in which one thing might be better than another while the opposite may be true on a general basis.

└> bt


Forum Etiquette: How to post data/code on a forum to get the best help
PaulB-TheOneAndOnly
PaulB-TheOneAndOnly
One Orange Chip
One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)

Group: General Forum Members
Points: 26559 Visits: 4639
bteraberry (6/30/2010)
PaulB-TheOneAndOnly (6/30/2010)
Gopal Rathore (6/30/2010)
Is this will be a good database design because it will make joins faster but again I will have to compromise with finding formulaId in Detail table as it will require another join to Master table of DocNum to find the corresponding formulaId?


Answer is embedded in the question :-)

To make joins work faster proposed design is adding I/O overhead - it defeats the purpose, isn't it?

I don't believe this is correct unless you assume poorly developed indexes.

As with everything in SQL there are many situation in which one thing might be better than another while the opposite may be true on a general basis.


Blink mmhhh... having problems to picture an scenario where performance improves by adding I/O.

_____________________________________
Pablo (Paul) Berzukov

Author of Understanding Database Administration available at Amazon and other bookstores.

Disclaimer: Advice is provided to the best of my knowledge but no implicit or explicit warranties are provided. Since the advisor explicitly encourages testing any and all suggestions on a test non-production environment advisor should not held liable or responsible for any actions taken based on the given advice.
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