我正在进行以下查询,该查询从具有不应存在于另一个表中的记录的表中提取数据。
查询工作正常,但耗时太多&表现受到极大影响。
我可以对此查询进行哪些更改以获得更好的性能,还是应该以其他方式执行此操作?
var data = (from A in ctx.tblMachine
where
A.CompanyId == companyId &&
A.InOutDate >= tempDt &&
A.InOutDate <= toDate &&
!(from B in ctx.tblEntry
where
B.CompanyId == companyId &&
A.EmployeeId == B.EmployeeId &&
A.InOutDate == B.EntryDate &&
B.EntryMethod == "M"
select new
{
B.EmployeeId
}).Contains(new { EmployeeId = A.EmployeeId })
orderby
A.EmployeeId, A.InOutDate select new
{
A.EmployeeId,
A.InOutDate,
A.InOutFlag,
A.InOutTime
}).ToList();
答案 0 :(得分:1)
您也可以尝试使用join而不是内部查询...您可以根据需要使用此类更改...
var data = (from A in ctx.tblMachine
join B in ctx.tblEntry on A.EmployeeId == B.EmployeeId &&
A.InOutDate == B.EntryDate &&
B.CompanyId == companyId &&
B.EntryMethod == "M"
where
A.CompanyId == companyId &&
A.InOutDate >= tempDt &&
A.InOutDate <= toDate &&
!(B.EmployeeId).Contains(new { EmployeeId = A.EmployeeId })
orderby
A.EmployeeId, A.InOutDate
select new
{
A.EmployeeId,
A.InOutDate,
A.InOutFlag,
A.InOutTime
}).ToList();