Linq 查询产生超时
Linq query generates timeouts
我有一个如下所示的查询:
var TheDataToDelete = (from x in MyDC.SomeTable
where x.....
select x).ToList();
if (TheDataToDelete.Count > 0)
{
MyDC.SomeTable.DeleteAllOnSubmit(TheDataToDelete);
MyDC.SubmitChanges();
}
大约有 10K 行要删除,在我的错误日志中我有时(每周一次)看到这个错误:
Inner Exception Type: System.ComponentModel.Win32Exception
Inner Exception: The wait operation timed out
Inner Source:
Exception Type: System.Data.SqlClient.SqlException
Exception: Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.
我的代码有什么问题,我需要更改什么来修复它?
按照@Shyju 的建议,使用Any()
。但也删除 ToList()
因为它已经执行了查询。不添加它会使查询延迟执行。因为这样只做 Any()
查询会更有效率。
var TheDataToDelete = (from x in MyDC.SomeTable
where x.....
select x);
if (TheDataToDelete.AnY())
{
MyDC.SomeTable.DeleteAllOnSubmit(TheDataToDelete);
MyDC.SubmitChanges();
}
这将产生 SQL 查询;
SELECT
(CASE
WHEN EXISTS(
SELECT x
FROM MyDC.SomeTable
WHERE x
) THEN 1
ELSE 0
END) AS [value]
快速高效。
我有一个如下所示的查询:
var TheDataToDelete = (from x in MyDC.SomeTable
where x.....
select x).ToList();
if (TheDataToDelete.Count > 0)
{
MyDC.SomeTable.DeleteAllOnSubmit(TheDataToDelete);
MyDC.SubmitChanges();
}
大约有 10K 行要删除,在我的错误日志中我有时(每周一次)看到这个错误:
Inner Exception Type: System.ComponentModel.Win32Exception
Inner Exception: The wait operation timed out
Inner Source:
Exception Type: System.Data.SqlClient.SqlException
Exception: Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.
我的代码有什么问题,我需要更改什么来修复它?
按照@Shyju 的建议,使用Any()
。但也删除 ToList()
因为它已经执行了查询。不添加它会使查询延迟执行。因为这样只做 Any()
查询会更有效率。
var TheDataToDelete = (from x in MyDC.SomeTable
where x.....
select x);
if (TheDataToDelete.AnY())
{
MyDC.SomeTable.DeleteAllOnSubmit(TheDataToDelete);
MyDC.SubmitChanges();
}
这将产生 SQL 查询;
SELECT
(CASE
WHEN EXISTS(
SELECT x
FROM MyDC.SomeTable
WHERE x
) THEN 1
ELSE 0
END) AS [value]
快速高效。