我需要向一个表中插入大量数据。相比于2005版本,SQL Server 2008是否有任何新功能能够提高性能?
我需要向一个表中插入大量数据。相比于2005版本,SQL Server 2008是否有任何新功能能够提高性能?
SQL Server 2008包含MERGE TSQL语句,可以加速某些类型的组合INSERT、UPDATE和DELETE操作。
如果您打算通过代码执行,我建议使用System.Data.SqlClient.SqlBulkCopy类(但也存在于SQL Server 2005中)。
我不知道这对你的问题是否可行,但如果可以的话,我真的建议你尝试用代码来开发。
我曾经有一个类似的问题,需要将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();
}
}