我正在加入Entity Framwork
中的两个对象,并将所有条件放在Where
子句中。但我的老板说要将Where
谓词中的所有其他条件放在更高效的位置。如下所示,第一个是我的版本 -
var query = from ct in dbContext.ClaimTrackings
join cts in dbContext.ClaimTrackingStatus
on ct.ID equals cts.ClaimTrackingID
where ct.CenterID == centerID && ct.ClaimMonth == SharePointClaimProcessingData.ClaimMonth && ct.ClaimYear == SharePointClaimProcessingData.ClaimYear
select cts;
var query = from ct in dbContext.ClaimTrackings.Where(p => p.CenterID == centerID && p.ClaimMonth == SharePointClaimProcessingData.ClaimMonth && p.ClaimYear == SharePointClaimProcessingData.ClaimYear)
join cts in dbContext.ClaimTrackingStatus
on ct.ID equals cts.ClaimTrackingID
select cts;
我无法看到生成的脚本,因为此代码只能在生产中运行。 我不确定哪一个更有效率。所以请解释一下。
答案 0 :(得分:0)
好的,我已经做了一些测试,我认为是正确的 - 因为结果是一样的,将where子句移到更高的位置根本没有任何区别 - 当然对于SQL服务器。
只是在我们的一个客户端应用程序的上下文中运行了这两个查询(刚刚换出了我的实体名称):
var query = from ct in _context.Batches
join cts in _context.Blends
on ct.BatchId equals cts.BatchId
where ct.BatchId == 13349 && ct.CreatedBy == "Automation"
select cts;
var query2 = from ct in _context.Batches.Where(x => x.BatchId == 13349 && x.CreatedBy == "Automation")
join cts in _context.Blends
on ct.BatchId equals cts.BatchId
select cts;
等效查询:
SELECT
[Extent2].[BlendId] AS [BlendId],
[Extent2].[BatchId] AS [BatchId],
[Extent2].[BlendNumber] AS [BlendNumber],
[Extent2].[ProductCode] AS [ProductCode],
[Extent2].[TargetQuantity] AS [TargetQuantity],
[Extent2].[RecipeDescription] AS [RecipeDescription],
[Extent2].[BlenderId] AS [BlenderId],
[Extent2].[TestingSetDescription] AS [TestingSetDescription],
[Extent2].[SieveDefaultDescription] AS [SieveDefaultDescription],
[Extent2].[ReblendTime] AS [ReblendTime],
[Extent2].[Operator] AS [Operator],
[Extent2].[ProcessedBy] AS [ProcessedBy],
[Extent2].[KeggedBy] AS [KeggedBy],
[Extent2].[ApprovedBy] AS [ApprovedBy],
[Extent2].[ActualQuantity] AS [ActualQuantity],
[Extent2].[TestedBy] AS [TestedBy],
[Extent2].[TestStatus] AS [TestStatus],
[Extent2].[ApprovedQuantity] AS [ApprovedQuantity],
[Extent2].[CreatedDate] AS [CreatedDate]
FROM [dbo].[Batches] AS [Extent1]
INNER JOIN [dbo].[Blends] AS [Extent2] ON [Extent1].[BatchId] = [Extent2].[BatchId]
WHERE (13349 = [Extent1].[BatchId]) AND (N'Automation' = [Extent1].[CreatedBy])
和
SELECT
[Extent2].[BlendId] AS [BlendId],
[Extent2].[BatchId] AS [BatchId],
[Extent2].[BlendNumber] AS [BlendNumber],
[Extent2].[ProductCode] AS [ProductCode],
[Extent2].[TargetQuantity] AS [TargetQuantity],
[Extent2].[RecipeDescription] AS [RecipeDescription],
[Extent2].[BlenderId] AS [BlenderId],
[Extent2].[TestingSetDescription] AS [TestingSetDescription],
[Extent2].[SieveDefaultDescription] AS [SieveDefaultDescription],
[Extent2].[ReblendTime] AS [ReblendTime],
[Extent2].[Operator] AS [Operator],
[Extent2].[ProcessedBy] AS [ProcessedBy],
[Extent2].[KeggedBy] AS [KeggedBy],
[Extent2].[ApprovedBy] AS [ApprovedBy],
[Extent2].[ActualQuantity] AS [ActualQuantity],
[Extent2].[TestedBy] AS [TestedBy],
[Extent2].[TestStatus] AS [TestStatus],
[Extent2].[ApprovedQuantity] AS [ApprovedQuantity],
[Extent2].[CreatedDate] AS [CreatedDate]
FROM [dbo].[Batches] AS [Extent1]
INNER JOIN [dbo].[Blends] AS [Extent2] ON [Extent1].[BatchId] = [Extent2].[BatchId]
WHERE (13349 = [Extent1].[BatchId]) AND (N'Automation' = [Extent1].[CreatedBy])
毫不奇怪他们是完全相同的。我无法为您提供EF用于生成查询的确切机制,但基本上您正在加入两个表并进行过滤,因此转换为商店查询会给您相同的结果,这很可能是因为它查看实体的方式参与
所以,在这种情况下,第二次猜测你的老板(我一直在做的事情)是正确的:)