Link to home
Start Free TrialLog in
Avatar of dthansen
dthansen

asked on

Primary Key on Temp Table

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)
Avatar of akku101
akku101
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER CERTIFIED SOLUTION
Avatar of Kevin Cross
Kevin Cross
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dthansen
dthansen

ASKER

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
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
                    )
                   
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
----
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.