问题涉及到SQL Server和C#。C#和SQL Server - 操作或服务器完成之前已超时的时间
我想在一个查询中执行多个删除语句。该格式具有以下结构:
DELETE from dbo.table where column = 'value'
重复超过10万次
我通过StringBuilder
构建命令,并调用它的方式在我的C#代码:但是它需要
cmd.Connection = con;
int rows = cmd.ExecuteNonQuery();
很多时间执行并以此错误结束:
The timeout period elapsed prior to completion of the operation or the server. Executing through Management Studio takes also a lot of time, and let me think that the query isn't performant enough. However, in this case the query is executed successfully.
显然,使用较少的DELETE
语句,由于查询非常简单,查询正常结束。执行多个语句并避免此错误的最佳方法是什么?
需要考虑的另一点:默认情况下,SQL Server使用**行级锁**,并仅锁定那些受例如一个'DELETE'语句。但是,如果您在单个事务中传递了5'000行,SQL Server将执行**锁升级**并将锁移至**整个表**,该表将成为**独占锁定**,直到事务完成。这意味着:在交易完成之前,没有人能够从该表中读取*。所以,也许你需要将你的DELETE操作批量化为更小的批次,以避免独占表锁 –