Home Forums SQL Server 2005 T-SQL (SS2K5) Primary Key creation - before and after bulk insert RE: Primary Key creation - before and after bulk insert

  • Do I create PK before or after the insert (or if it depends, what are the factors)?

    I would definitely create my indexes/PK's after the data is loaded. That way the statistics will be up to date and provide a better query plan. From what you said, your own testing has verified this. There is one issue which I have run into, and that is when the table is created and the indexes/statistics are created - If you need to delete and reload the data, then the queryplan can be way of. Also, I have run into an issue where deleting the data from a table and recompiling the stored procedures on that table, whether temp or permanent, the query plan has been way off.

    If I use table instead of temporary table, is the behavior similar?

    This one should be based upon your system. A Temp Table (#<tablename) is actually created in tempdb, and thus on the drive where your tempdb is located. If you have a very busy system, then there will definitely be an I/O issue as other processes will be competing for disk space and IO. You would have to test various configurations to determine the best. Optins could be to create a temp table, a permanent table, maybe a file group on another drive and place the table on that. These are just of the top of my head.

    Good luck

    Steve Jimmo
    Sr DBA
    “If we ever forget that we are One Nation Under God, then we will be a Nation gone under." - Ronald Reagan