我在C#项目中使用了SqlTransaction
,我使用Delete
语句并进行EcexuteNonQuery
调用。
这非常有效并且我总是要删除相同数量的行,但是95%的时间,这需要1毫秒和大约5%的时间,它在300到500毫秒之间。
我的代码:
using (SqlTransaction DbTrans = conn.BeginTransaction(IsolationLevel.ReadCommitted))
{
SqlCommand dbQuery = conn.CreateCommand();
dbQuery.Transaction = DbTrans;
dbQuery.CommandType = CommandType.Text;
dbQuery.CommandText = "delete from xy where id = @ID";
dbQuery.Parameters.Add("ID", SqlDbType.Int).Value = x.ID;
dbQuery.ExecuteNonQuery();
}
我的代码有问题吗?
答案 0 :(得分:3)
阅读Understanding how SQL Server executes a query和How to analyse SQL Server performance,让您开始解决此类问题。
当然我假设你有xy.id
的索引。您的DELETE可能会不时阻止。这可能是由多种原因造成的:
它的要点是使用上面链接的文章(特别是第二篇文章)中的技术,你可以找出原因并适当地解决它。
如果有的话,对C#代码的更改几乎没有影响。使用存储过程是 没有帮助。你需要根本导致问题。