实体框架简单子查询生成丑陋的sql

时间:2014-02-12 18:32:05

标签: sql-server entity-framework linq-to-sql

我尝试优化以下简单的Entity Framework查询以从同一组中恢复所有产品

var query = from p in ctx.Products 
            where p.GroupId == ( from q in ctx.Products 
                                 where q.Id == new Guid(".....")                                       
                                 select q.GroupId).FirstOrDefault()
            select p;

使用SQL Server Profiler检查生成的sql后,我看到了这个查询

SELECT 
[Extent1].[Id] AS [Id], 
[Extent1].[GroupId] AS [GroupId], 
[Extent1].[Name] AS [Name], 
[Extent1].[Code] AS [Code]
FROM       [inv].[Products] AS [Extent1]
LEFT OUTER JOIN  (SELECT TOP (1) [Extent2].[GroupId] AS [GroupId]
    FROM [inv].[Products] AS [Extent2]
    WHERE cast('.....' as uniqueidentifier) = [Extent2].[Id] ) AS [Limit1] ON 1 = 1
LEFT OUTER JOIN  (SELECT TOP (1) [Extent3].[GroupId] AS [GroupId]
    FROM [inv].[Products] AS [Extent3]
    WHERE cast('.....' as uniqueidentifier) = [Extent3].[Id] ) AS [Limit2] ON 1 = 1
LEFT OUTER JOIN  (SELECT TOP (1) [Extent4].[GroupId] AS [GroupId]
    FROM [inv].[Products] AS [Extent4]
    WHERE cast('.....' as uniqueidentifier) = [Extent4].[Id] ) AS [Limit3] ON 1 = 1
LEFT OUTER JOIN  (SELECT TOP (1) [Extent5].[GroupId] AS [GroupId]
    FROM [inv].[Products] AS [Extent5]
    WHERE cast('.....' as uniqueidentifier) = [Extent5].[Id] ) AS [Limit4] ON 1 = 1
LEFT OUTER JOIN  (SELECT TOP (1) [Extent6].[Id] AS [Id]
    FROM [inv].[Products] AS [Extent6]
    WHERE cast('.....' as uniqueidentifier) = [Extent6].[Id] ) AS [Limit5] ON 1 = 1
LEFT OUTER JOIN  (SELECT TOP (1) [Extent7].[Id] AS [Id]
    FROM [inv].[Products] AS [Extent7]
    WHERE cast('.....' as uniqueidentifier) = [Extent7].[Id] ) AS [Limit6] ON 1 = 1
WHERE ([Extent1].[GroupId] = (CASE WHEN ([Limit1].[GroupId] IS NULL) 
                                   THEN cast('00000000-0000-0000-0000-000000000000' as uniqueidentifier) 
                                   ELSE [Limit2].[GroupId] END)) 
AND (CASE WHEN ([Limit3].[GroupId] IS NULL) 
          THEN cast('00000000-0000-0000-0000-000000000000' as uniqueidentifier) 
          ELSE [Limit4].[GroupId] END IS NOT NULL)

为什么会产生这么多相同的JOIN? 是否有任何选项可以删除最终的空投射? 你能否提出改进生成的sql的方法?

我已经使用LINQPad进行了测试,并且正在生成正常的SQL

-- Region Parameters
DECLARE @p0 UniqueIdentifier = '....'
-- EndRegion
SELECT [t0].[Id], [t0].[GroupId], [t0].[Name], [t0].[Description], [t0].[Code]
FROM [inv].[Products] AS [t0]
WHERE [t0].[GroupId] = ((
    SELECT TOP (1) [t1].[GroupId]
    FROM [inv].[Products] AS [t1]
    WHERE [t1].[Id] = @p0
    ))

2 个答案:

答案 0 :(得分:1)

如果您真的在这里处理主键(id),我不确定您是否需要FirstOrDefault。以下是否会实现相同的目标:

var query = from groupProduct in ctx.Products 
            where groupProduct.Id == someGuid // This should return only one result
            join childProduct in ctx.Products
            on groupProduct.Id equals childProduct.GroupId
            select childProduct

生成的SQL是

SELECT 
[Extent2].[Id] AS [Id], 
[Extent2].[GroupId] AS [GroupId], 
[Extent2].[Name] AS [Name], 
[Extent2].[Code] AS [Code]
FROM  [inv].[Products] AS [Extent1]
INNER JOIN [inv].[Products] AS [Extent2] ON [Extent1].[Id] = [Extent2].[GroupId]
WHERE cast('....' as uniqueidentifier) = [Extent1].[Id]

答案 1 :(得分:0)

尝试以下两个语句,但是如果你想要的话,应该产生更顺畅的SQL:

var groupId = ctx.Products.Where(p=> p.Id = new Guid(...).Select(p=> p.GroupId).FirstOrDefault();

if(groupId.HasValue)
{
   var query = ctx.Products.Where(p=> p.GroupId == groupId).Select(p=> p);
}

这假设Id是一个int,如果不是,则使用适当的边界检查。