Sql server 2008 - 用于插入大量数据的性能调整功能

时间:2010-10-20 02:19:37

标签: sql-server sql-server-2008 performance

我必须在表格中插入大量数据。 sqlserver 2008(与2005相比)是否有任何新功能可以提高性能?

2 个答案:

答案 0 :(得分:4)

SQL Server 2008包含MERGE TSQL语句,可以加速某些类型的INSERT,UPDATE和DELETE组合操作。

如果您打算执行代码,我建议使用System.Data.SqlClient.SqlBulkCopy类(但也出现在SQL Server 2005中)。

答案 1 :(得分:1)

我不知道这对您的问题是否可行,但如果可以,我会尝试在代码中进行开发。

我过去曾对一个大项目提出过类似的问题,需要将15年的生产数据导入新的模式(在SQL Server 2005中)。 System.Data.SqlClient.SqlBulkCopy是迄今为止最快的选择。

如果你这样做,我建议一次大量插入大约1 GB的插件,然后手动调用.NET GC以释放内存中的表。我不得不做这两件事而不会遇到内存错误(不过32位系统。)

编辑 - 我的解决方案的伪代码类似于:

Table dataToInsert = new Table();
var sqlCommand = new SqlCommand("select * from old database");
DataReader dataFromOldSystem = sqlCommand.ExecuteReader();
foreach (DataRow oldRow in dataFromOldSystem.Tables[0])
{
// I had to modify/transpose the row from the old table in some way
DataRow newRow = new DataRow(oldRow.GetInt(0), oldRow.GetDateTime(1), oldRow.GetInt(2));
dataToInsert.AddRow(newRow);

newRow = new DataRow(oldRow.GetInt(0), oldRow.GetDateTime(1), oldRow.GetInt(3));
dataToInsert.AddRow(newRow);

newRow = new DataRow(oldRow.GetInt(0), oldRow.GetDateTime(1), oldRow.GetInt(4));
dataToInsert.AddRow(newRow);

// check if the number of rows is over some magic number that is below the memory limit
// you can check the private bytes in use by your app to help guess this number
if (dataToInsert.Rows.Count > 1000000)
{
SqlBulkCopy bulkCopier = new BulkCopy(blah);
bulkCopier.Execute();

dataToInsert = null;
GC.Finalize();
GC.Free;

dataToInsert = new Table();
}
}
相关问题