How to reduce time in adding PK constraint on a table?

  • Oh wow. I didn't realize that. To be perfectly honest, I'd never created a PK constraint in this fashion before, so I just assumed that the index that was created when you normally created a PK had more to do with the clustered index than the PK constraint. It all makes sense now. So, I can go and create the non-clustered unique index leaving the table online, then just add the constraint QUICKLY afterwards.

  • Doing it something like this:

    alter table MyTable

    Add Constraint [MyPrimaryKey] Primary Key (MyColumn)

    WITH (FILLFACTOR = 75, ONLINE = ON, PAD_INDEX = ON);

    Be sure to verify the complete syntax in BOL.

    You may also want the NOCHECK option.

    Again, check the syntax in BOL.

  • coronaride (7/3/2012)


    So, I can go and create the non-clustered unique index leaving the table online, then just add the constraint QUICKLY afterwards.

    Err... No.

    When you create a constraint, SQL creates the enforcing index for you. It's an integral part of creating a constraint. If you create an index, then create the constraint you will end up with 2 indexes.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • Ok. You're right, of course. That's obviously what happened in my test environment. But riddle me this - why was it that it took me only 20 seconds to add the constraint (as well as the second index, apparently) *after* I added the (first) non-clustered unique index? All previous attempts were well over 5 minutes.

  • Because SQL can read the existing index to create the new one whereas without that inde it would have had to read the entire table.

    Compare the time that creating the index + creating the constraint took vs just creating the constraint

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • Ok, and the bulk of the time required for creating the constraint is creating the enforcing index? Alright, makes sense. Thank you, and thank everyone else on this thread.

  • Whew, I was on the edge of my seat while reading this. Wasn't sure if we were ever going to get here. ๐Ÿ˜€

  • coronaride (7/3/2012)


    Ok, and the bulk of the time required for creating the constraint is creating the enforcing index?

    Yes. All there is to a constraint is an index and some metadata.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • Lynn Pettis (7/3/2012)


    Doing it something like this:

    alter table MyTable

    Add Constraint [MyPrimaryKey] Primary Key (MyColumn)

    WITH (FILLFACTOR = 75, ONLINE = ON, PAD_INDEX = ON);

    Be sure to verify the complete syntax in BOL.

    You may also want the NOCHECK option.

    Again, check the syntax in BOL.

    Assuming tempdb is reasonbly tuned, I would add the:

    SORT_IN_TEMPDB = ON

    option to the build, i.e.:

    WITH (FILLFACTOR = 75, ONLINE = ON, PAD_INDEX = ON, SORT_IN_TEMPDB = ON);

    That could (really should?) help reduce the time (altho it will likely use more overall total disk space during the creation process (only -- the space will be released on the build is complete)).

    Also, make sure you have pre-allocated (and thus pre-formatted) space available in the db's log file before you issue the command, particularly if you have a small log autogrow amount or, worse, a percentage growth.

    SQL DBA,SQL Server MVP(07, 08, 09) "Money can't buy you happiness." Maybe so, but it can make your unhappiness a LOT more comfortable!

  • Thank you.

  • Alright. Educational session in understanding what a PK constraint actually is today. Thanks a lot.

  • coronaride (7/3/2012)


    Alright. Educational session in understanding what a PK constraint actually is today. Thanks a lot.

    Everyone's a winner. Thanks for the feedback, coronaride.

    โ€œWrite the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.โ€ - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

Viewing 12 posts - 31 through 41 (of 41 total)

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