Solved

Primary Key on Temp Table

Posted on 2011-10-01
9
389 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
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.

 
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: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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
migrate a SQL 2008 to 2016, 2 26
SQL Server 2012 r2 - Query results have 2 seperate results instead of 1 2 21
Sql server function help 15 27
SQL view 2 26
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
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…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

816 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

10 Experts available now in Live!

Get 1:1 Help Now