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 12»»

Candidate Key Or Composite Key Expand / Collapse
Author
Message
Posted Friday, June 25, 2010 8:47 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, June 29, 2010 11:56 PM
Points: 36, 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?
Post #943101
Posted Tuesday, June 29, 2010 7:28 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Monday, October 1, 2012 3:30 PM
Points: 292, Visits: 1,028
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

Post #944632
Posted Tuesday, June 29, 2010 7:51 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Friday, December 12, 2014 10:09 AM
Points: 2,876, Visits: 5,201
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 ). 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 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) . 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!"
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
Post #944662
Posted Tuesday, June 29, 2010 11:04 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Tuesday, January 28, 2014 8:15 AM
Points: 3,065, Visits: 4,639
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.
Post #944848
Posted Tuesday, June 29, 2010 12:27 PM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Friday, November 14, 2014 4:49 PM
Points: 1,093, Visits: 1,175
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
Post #944936
Posted Wednesday, June 30, 2010 12:00 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, June 29, 2010 11:56 PM
Points: 36, 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?
Post #945216
Posted Wednesday, June 30, 2010 6:55 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Tuesday, January 28, 2014 8:15 AM
Points: 3,065, Visits: 4,639
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.
Post #945430
Posted Wednesday, June 30, 2010 7:47 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Friday, December 12, 2014 10:09 AM
Points: 2,876, Visits: 5,201
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 .
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!"
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
Post #945482
Posted Wednesday, June 30, 2010 11:19 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Friday, November 14, 2014 4:49 PM
Points: 1,093, Visits: 1,175
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
Post #945669
Posted Wednesday, June 30, 2010 12:09 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Tuesday, January 28, 2014 8:15 AM
Points: 3,065, Visits: 4,639
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.


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.
Post #945699
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse