以下查询应返回约7200条记录:
using (var context = new RapEntities())
{
context.Configuration.ProxyCreationEnabled = false;
var query = from i in context.QbTxnItems.AsNoTracking()
where (i.ListType == "Invoice")
&& !context.Payments.Any(p => p.QbTxnId == i.QbTxnId && p.QbTxnId != null)
&& !context.QbTxnIgnores.Any(ti => ti.QbTxnId == i.QbTxnId)
orderby i.RefNumber
select i;
var items = RapEntities.GetList(query);
生成的sql(来自sql server profiler:
SELECT
[Extent1].[QbTxnItemId] AS [QbTxnItemId],
[Extent1].[ListType] AS [ListType],
[Extent1].[QbTxnId] AS [QbTxnId],
[Extent1].[QbEditSequence] AS [QbEditSequence],
[Extent1].[TxnNumber] AS [TxnNumber],
[Extent1].[RefNumber] AS [RefNumber],
[Extent1].[TxnDate] AS [TxnDate],
[Extent1].[TxnAmt] AS [TxnAmt],
[Extent1].[IsPaid] AS [IsPaid],
[Extent1].[IsCleared] AS [IsCleared],
[Extent1].[LastGetAll] AS [LastGetAll],
[Extent1].[GetIsCleared] AS [GetIsCleared],
[Extent1].[LastModified] AS [LastModified],
[Extent1].[Version] AS [Version],
[Extent1].[RecordStatus] AS [RecordStatus],
[Extent1].[UserId] AS [UserId],
[Extent1].[TableID] AS [TableID]
FROM [dbo].[QbTxnItems] AS [Extent1]
WHERE (N'Invoice' = [Extent1].[ListType]) AND ( NOT EXISTS (SELECT
1 AS [C1]
FROM [dbo].[Payments] AS [Extent2]
WHERE ([Extent2].[QbTxnId] = [Extent1].[QbTxnId]) AND ([Extent2].[QbTxnId] IS NOT NULL)
)) AND ( NOT EXISTS (SELECT
1 AS [C1]
FROM [dbo].[QbTxnIgnores] AS [Extent3]
WHERE [Extent3].[QbTxnId] = [Extent1].[QbTxnId]
))
ORDER BY [Extent1].[RefNumber] ASC
在从实体框架执行时不会在任何合理的时间内完成,但会立即从SSMS执行。
使用take(200)将行数限制为200,即使从EF调用,查询也会运行大约50毫秒。将行数增加到500会将时间增加到超过5秒。
这似乎是不恰当的表现。 EF必须能够在合理的时间内返回超过几百行。是否有任何可以调整的设置可以提高从EF运行更大查询的能力?
答案 0 :(得分:1)
在我看来,你可以通过在这里实际做一些连接来加快速度。 试试这个:
var query = (from i in context.QbTxnItems.AsNoTracking()
join p in context.Payments on i.QbTxnId equals p.QbTxnId
join qi in context.QbTxnIgnores on i.QbTxnId equals qi.QbTxnId
where (i.ListType == "Invoice")
select i).OrderBy(i => i.RefNumber);