Solved

Primary Key on Temp Table

Posted on 2011-10-01
9
384 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
Comment Utility
0
 
LVL 59

Accepted Solution

by:
Kevin Cross earned 333 total points
Comment Utility
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
Comment Utility
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
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
Comment Utility
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
Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

 
LVL 75

Assisted Solution

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

Author Comment

by:dthansen
Comment Utility
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
Comment Utility
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:EugeneZ
Comment Utility
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
Comment Utility
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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Join & Write a Comment

Suggested Solutions

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.

744 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now