I’m using SQL Server 2008 and I am running several (15) scripts each day to bulk insert to tables. Each script drops the table at the beginning. I create indexes in the scripts. Some of the scripts reference the tables that were created with previous scripts. I want to improve the bulk insert operations but also want to keep the indexes to improve query performances. Does it make sense to create the indexes using maintenance plan rather than creating them in the script? Will it improve the bulk insert performance if I create the indexes in the maintenance plan at the end of all scripts run? Thanks.
Sql-server – Creating indexes with t-sql scrips vs rebuild indexes in maintenance plan
bulk-insertperformancesql serversql-server-2008
Related Question
- Sql-server – Differences Between Two Different Create Index Commands
- SQL Server 2008 – Recommended DBA Tasks and Maintenance Plan
- SQL Server – Why SqlBulkCopy Slows with Table Row Count
- SQL Server 2014 – How to Investigate BULK INSERT Performance
- Sql-server – TSQL – SQL Server 2008 Maintenance Plan – anything else
- Sql-server – Create a maintenance plan for sql database with C#
- Sql-server – Unable to Create new Maintenance plan in SSMS 2017
Best Answer
Removing the impact of updating your non-clustered indexes during the inserts is a great way to improve bulk insert performance and something we do in my own environments. However, I think a better alternative to you than dropping the table and/or indexes, then recreating them post-insert is to instead disable then rebuild the index. This process removes some over head of the index creation, but accomplishes the same goal of getting the indexes out of the way.
The command to do this would be:
ALTER INDEX foo ON bar DISABLE
ALTER INDEX foo ON bar REBUILD
Depending on how you execute your ETL loads, you can build this as two steps and script out the index disable and rebuild (SQL 2008 syntax):
Note, by declaring
type=2
, we are only disabling/rebuilding non-clustered indexes. You don't want to disable/rebuild your clustered indexes as part of this process.