我试图找出通过 SQL Server 中的迷你控制台应用程序执行批量更新的最佳方法。我已经编写了自己的批量更新方式,如下所示:
SqlCommand command = new SqlCommand();
command.Connection = new SqlConnection("Data Source=.;Initial Catalog=mydb;Integrated Security=SSPI");
command.Connection.Open();
for (int i = 0; i < items.Count; i = i + 1000)
{
var batchList = items.Skip(i).Take(1000).ToList();
for (int j = 0; j < batchList.Count(); j++)
{
command.CommandText += string.Format("update Items set QuantitySold=@s_id{0} where ItemID = @id{0};", j);
command.Parameters.AddWithValue("@s_id" + j, batchList[j].QuantitySold);
command.Parameters.AddWithValue("@id" + j, batchList[j].ItemID);
}
command.ExecuteNonQuery();
command = new SqlCommand();
command.Connection = new SqlConnection("Data Source=.;Initial Catalog=mydb;Integrated Security=SSPI");
command.Connection.Open();
}
command.Connection.Close();
但是我对这个的性能不太满意,更新我的数据库中的 50000-100000 条记录在这样做时会变得很慢,即使它是分批 1000 条......
有没有可以“加快速度”的库/解决方案?
有人可以帮我吗 ?