July 3, 2012 at 11:24 am
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.
July 3, 2012 at 11:28 am
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.
July 3, 2012 at 11:51 am
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
July 3, 2012 at 12:00 pm
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.
July 3, 2012 at 12:02 pm
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
July 3, 2012 at 12:07 pm
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.
July 3, 2012 at 12:24 pm
Whew, I was on the edge of my seat while reading this. Wasn't sure if we were ever going to get here. ๐
July 3, 2012 at 1:14 pm
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
July 3, 2012 at 1:49 pm
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) "It's a dog-eat-dog world, and I'm wearing Milk-Bone underwear." "Norm", on "Cheers". Also from "Cheers", from "Carla": "You need to know 3 things about Tortelli men: Tortelli men draw women like flies; Tortelli men treat women like flies; Tortelli men's brains are in their flies".
July 3, 2012 at 2:03 pm
Thank you.
July 3, 2012 at 2:04 pm
Alright. Educational session in understanding what a PK constraint actually is today. Thanks a lot.
July 4, 2012 at 1:52 am
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.
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