views:

1696

answers:

7

I have a stored procedure that is working with a large amount of data. I have that data being inserted in to a temp table. The overall flow of events is something like

CREATE #TempTable (
    Col1 NUMERIC(18,0) NOT NULL,    --This will not be an identity column.
    ,Col2 INT NOT NULL,
    ,Col3 BIGINT,

    ,Col4 VARCHAR(25) NOT NULL,
    --Etc...

    --
    --Create primary key here?
)


INSERT INTO #TempTable
SELECT ...
FROM MyTable
WHERE ...

INSERT INTO #TempTable
SELECT ...
FROM MyTable2
WHERE ...

--
-- ...or create primary key here?

My question is when is the best time to create a primary key on my #TempTable table? I theorized that I should create the primary key constraint/index after I insert all the data because the index needs to be reorganized as the primary key info is being created. But I realized that my underlining assumption might be wrong...

In case it is relevant, the data types I used are real. In the #TempTable table, Col1 and Col4 will be making up my primary key.

Update: In my case, I'm duplicating the primary key of the source tables. I know that the fields that will make up my primary key will always be unique. I have no concern about a failed alter table if I add the primary key at the end.

Though, this aside, my question still stands as which is faster assuming both would succeed? Thank you to everyone who has and will reply...

Regards,
Frank

P.S. I'm sorry if this is a duplicate. It is basic enough that it could be but I've not been able to find anything like it.

+3  A: 

You may as well create the primary key before the inserts - if the primary key is on an identity column then the inserts will be done sequentially anyway and there will be no difference.

Kragen
I will not be using identity. You posted this while I was updating my question. My primary key will consist of the NUMERIC(18,0) and a VARCHAR(25) field.
Frank V
Primary Keys are Clustered by default. This will order your sequentially order the data based upon the PK Values. I agree with this answer, it should happen before you insert. Also note: if you add extra non-clustered indexes. Creating a clustered PK after would cause SQL Server to rebuild the non-clustered indexes.
DBAndrew
A: 

I don't think it makes any significant difference in your case:

  • either you pay the penalty a little bit at a time, with each single insert
  • or you'll pay a larger penalty after all the inserts are done, but only once

When you create it up front before the inserts start, you could potentially catch PK violations as the data is being inserted, if the PK value isn't system-created.

But other than that - no big difference, really.

Marc

marc_s
extent splits and logging and stuff? This doesn't need to be taken in to account?
Frank V
A: 

If you add the primary key when creating the table, the first insert will be free (no checks required.) The second insert just has to see if it's different from the first. The third insert has to check two rows, and so on. The checks will be index lookups, because there's a unique constraint in place.

If you add the primary key after all the inserts, every row has to be matched against every other row. So my guess is that adding a primary key early on is cheaper.

But maybe Sql Server has a really smart way of checking uniqueness. So if you want to be sure, measure it!

Andomar
Didn't think of that one... that is good.
Frank V
A: 

I wasn't planning to answer this, since I'm not 100% confident on my knowledge of this. But since it doesn't look like you are getting much response ...

My understanding is a PK is a unique index and when you insert each record, your index is updated and optimized. So ... if you add the data first, then create the index, the index is only optimized once.

So, if you are confident your data is clean (without duplicate PK data) then I'd say insert, then add the PK.

But if your data may have duplicate PK data, I'd say create the PK first, so it will bomb out ASAP.

John MacIntyre
Thank you for your reply. I am, in fact, sure that I won't have a duplicates issue...
Frank V
A: 

Even more important than performance considerations, if you are not ABSOLUTELY, 100% sure that you will have unique values being inserted into the table, create the primary key first. Otherwise the primary key will fail to be created.

This prevents you from inserting duplicate/bad data.

Jeff Meatball Yang
This isn't a problem for me. I understand it might be for some, but not a problem at all for me.
Frank V
+2  A: 

This depends a lot.

If you make the primary key index clustered after the load, the entire table will be re-written as the clustered index isn't really an index, it is the logical order of the data. Your execution plan on the inserts is going to depend on the indexes in place when the plan is determined, and if the clustered index is in place, it will sort prior to the insert. You will typically see this in the execution plan.

If you make the primary key a simple constraint, it will be a regular (non-clustered) index and the table will simply be populated in whatever order the optimizer determines and the index updated.

I think the overall quickest performance (of this process to load temp table) is usually to write the data as a heap and then apply the (non-clustered) index.

However, as others have noted, the creation of the index could fail. Also, the temp table does not exist in isolation. Presumably there is a best index for reading the data from it for the next step. This index will need to either be in place or created. This is where you have to make a tradeoff of speed here for reliability (apply the PK and any other constraints first) and speed later (have at least the clustered index in place if you are going to have one).

Cade Roux
Interesting. Thank you. Helpful. Could you expand with some examples perhaps?
Frank V
@Cade, for the clustered index, you mean physical order on disk, not logical order (tables don't have a logical order).
Peter
No, physical order on disk can be anything. The clustered index is simply the data stored in the leaves in a btree index instead of in a heap. There still can be fragementation in SQL Server and on disk.
Cade Roux
I see what you mean. Point taken.
Peter
A: 

If the recovery model of your database is set to simple or bulk-logged, SELECT ... INTO ... UNION ALL may be the fastest solution. SELECT .. INTO is a bulk operation and bulk operations are minimally logged.

eg:

-- first, create the table
SELECT ...
INTO #TempTable
FROM MyTable
WHERE ...
UNION ALL
SELECT ...
FROM MyTable2
WHERE ...

-- now, add a non-clustered primary key:
-- this will *not* recreate the table in the background
-- it will only create a separate index
-- the table will remain stored as a heap
ALTER TABLE #TempTable ADD PRIMARY KEY NONCLUSTERED (NonNullableKeyField)

-- alternatively:
-- this *will* recreate the table in the background
-- and reorder the rows according to the primary key
-- CLUSTERED key word is optional, primary keys are clustered by default
ALTER TABLE #TempTable ADD PRIMARY KEY CLUSTERED (NonNullableKeyField)

Otherwise, Cade Roux had good advice re: before or after.

Peter