Solved

Primary Key on Temp Table

Posted on 2011-10-01
9
396 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 2

Expert Comment

by:akku101
ID: 36898110
0
 
LVL 60

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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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 60

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 43

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

751 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