Solved

Primary Key on Temp Table

Posted on 2011-10-01
9
385 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
 
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
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
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:EugeneZ
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

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.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how the fundamental information of how to create a table.

863 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

26 Experts available now in Live!

Get 1:1 Help Now