Candidate Key Or Composite Key

  • 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?

  • 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

  • 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 😀 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[/url]

  • 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.
  • 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[/url]

  • 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?

  • 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.
  • 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[/url]

  • 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[/url]

  • 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.
  • PaulB-TheOneAndOnly (6/30/2010)


    ...

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

    I don't think performance is the only concern when designing a database. Would you denormalize everything in every database to avoid using joins and reduce I/O? It is acceptable in database used for reporting purposes, but what about OLTP one?

    _____________________________________________
    "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[/url]

  • PaulB-TheOneAndOnly (6/30/2010)


    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.

    The idea that you're necessarily adding I/O is based on the assumption that the indexes are poorly constructed.

    Obviously there are times when that would be the case but I feel that you're oversimplifying the case to make your point. There are many times when an artificial numeric key can be used in conjunction with other data within the same index to result in better join performance while having no negative impact on I/O.

    └> bt



    Forum Etiquette: How to post data/code on a forum to get the best help[/url]

Viewing 12 posts - 1 through 11 (of 11 total)

You must be logged in to reply to this topic. Login to reply