Solved

Primary Key on Temp Table

Posted on 2011-10-01
9
395 Views
Last Modified: 2012-05-12
I have the following temp table which gets populated with between 1,000,000 and 10,000,000 rows after it has been created.

Which is for efficient, to add the PK after we insert the rows or before we insert the rows?

Thanks.

create table #CallsToBill (
      CallID char(15) COLLATE Latin1_General_BIN NOT NULL,
      CallCost numeric (9, 7) NOT NULL,
      CallTax numeric (9, 7) NOT NULL,
      CallTotal numeric (9, 7) NOT NULL,
      StartTime datetime NOT NULL,
      Direction char(1) NOT NULL,
      BilledTier smallint,
      BilledDuration bigint,
      CallType tinyint,
      Period varchar(50) NOT NULL
                    )
                    
ALTER TABLE #CallsToBill ADD PRIMARY KEY (CallID)
0
Comment
Question by:dthansen
9 Comments
 
LVL 2

Expert Comment

by:akku101
ID: 36898110
0
 
LVL 59

Accepted Solution

by:
Kevin Cross earned 333 total points
ID: 36898141
You are pretty close with what you have.

ALTER TABLE #CallsToBill ADD CONSTRAINT
   PK_CallsToBill PRIMARY KEY CLUSTERED(CallID)
   WITH FILLFACTOR = 100;

Here is the BOL: http://msdn.microsoft.com/en-us/library/ms190273.aspx
0
 

Author Comment

by:dthansen
ID: 36898153
I under the FILLFACTOR to 100 is a good idea. Thank you for that.

What neither posted link tells me is a clear opinion on the order of the insert. i.e., insert data then create PK or create PK then insert data.

The list from akku101 has opinions in both directions but none of them conclusive.

I don't see any opinion on order in the mwvisa1 link.

Thanks,
Dean
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 36898155
or like this

create table #CallsToBill (
      CallID char(15) COLLATE Latin1_General_BIN NOT NULL PRIMARY KEY,
      CallCost numeric (9, 7) NOT NULL,
      CallTax numeric (9, 7) NOT NULL,
      CallTotal numeric (9, 7) NOT NULL,
      StartTime datetime NOT NULL,
      Direction char(1) NOT NULL,
      BilledTier smallint,
      BilledDuration bigint,
      CallType tinyint,
      Period varchar(50) NOT NULL
                    )
                   
0
 
LVL 75

Assisted Solution

by:Anthony Perkins
Anthony Perkins earned 167 total points
ID: 36898160
If you have an option add the Primary Key after doing the INSERT.
0
 

Author Comment

by:dthansen
ID: 36898173
I do have an option. acperkins, can you provide a one-liner on why we would do the PK after the insert? Just want to understand the reason behind it.

Thanks,
Dean
0
 
LVL 59

Assisted Solution

by:Kevin Cross
Kevin Cross earned 333 total points
ID: 36898187
I am sorry, Dean. I thought that was already established in why you were asking how to add the PRIMARY KEY after the table and data is INSERTed already. I see now that is a secondary question in your post. The reason is that on INSERT with primary key, the constraint has to be checked and statistics for the index updated. Both are adding overhead to the INSERT. You are adding in 1-10M rows, so you want this as efficient as possible. So one liner: it is more efficient to do CREATE TABLE, INSERT, ADD PRIMARY KEY/INDEX.
0
 
LVL 42

Expert Comment

by:Eugene Z
ID: 36899407
what is you sql server version \edition?

if you are on sql2005/2008
instead of Create table\INSERT INTO
 use  faster SELECT INTO table (temp or regular -- > for 10M can be a good idea to use some user DB "temp" normal table..)
after such table is created  add not just clustered but non-clustered indexes as well (depends on your plans to query this table =>what will be in "Where" clause for example)
..


--
BTW:if you are using sql 2000
when you create PK: By default, a nonclustered index is created if the clustering option is not specified.
----
0
 
LVL 60

Expert Comment

by:chapmandew
ID: 36906630
It depends on what you mean by "effecient".  If you mean faster to load data into the table, then add the clustered key after the data load.  However, depending on your data you load, data integrity can be compromised by doing this.

Remember...a PK is a constraint while a clustered index is for data retrieval.  PK is used to identify unique records in a table, a clustered key orders the table based on the key.
0

Featured Post

Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question