L2E:GroupBy 总是被翻译成 Distincts,而不是 Group By

发布于 2024-10-25 10:00:37 字数 1584 浏览 1 评论 0原文

如何让 Linq-to-Entities 提供程序真正执行 GROUP BY?无论我做什么,它总是生成比实际的 GROUP BY 慢得多的 SQL。例如:

var foo = (from x in Context.AccountQuantities
  where x.AccountID == 27777
  group x by x.StartDate
  into groups
  select new
    {
      groups.FirstOrDefault().StartDate,
      Quantity = groups.Max(y => y.Quantity)
    }).ToList();

翻译为:

SELECT 
1 AS [C1], 
[Project4].[C1] AS [C2], 
[Project4].[C2] AS [C3]
FROM ( SELECT 
    [Project3].[C1] AS [C1], 
    (SELECT 
        MAX([Extent3].[Quantity]) AS [A1]
        FROM [dbo].[AccountQuantities] AS [Extent3]
        WHERE (27777 = [Extent3].[AccountID]) AND ([Project3].[StartDate] = [Extent3].[StartDate])) AS [C2]
    FROM ( SELECT 
        [Distinct1].[StartDate] AS [StartDate], 
        (SELECT TOP (1) 
            [Extent2].[StartDate] AS [StartDate]
            FROM [dbo].[AccountQuantities] AS [Extent2]
            WHERE (27777 = [Extent2].[AccountID]) AND ([Distinct1].[StartDate] = [Extent2].[StartDate])) AS [C1]
        FROM ( SELECT DISTINCT 
            [Extent1].[StartDate] AS [StartDate]
            FROM [dbo].[AccountQuantities] AS [Extent1]
            WHERE 27777 = [Extent1].[AccountID]
        )  AS [Distinct1]
    )  AS [Project3]
)  AS [Project4]

我怎样才能让它执行这个?

SELECT
AccountQuantities.StartDate,
MAX(AccountQuantities.Quantity)
FROM AccountQuantities
WHERE AccountID=27777
GROUP BY StartDate

再次注意,执行的内容中缺少 ANY GROUP BY。我对 EF 非理想优化很满意,但这会慢几个数量级,我找不到任何方法说服它真正执行 GROUP BY,这对我们来说是一个主要问题!

How can I get the Linq-to-Entities provider to truly perform a GROUP BY? No matter what I do, it always generates SQL that is far slower than an actual GROUP BY. For example:

var foo = (from x in Context.AccountQuantities
  where x.AccountID == 27777
  group x by x.StartDate
  into groups
  select new
    {
      groups.FirstOrDefault().StartDate,
      Quantity = groups.Max(y => y.Quantity)
    }).ToList();

translates to:

SELECT 
1 AS [C1], 
[Project4].[C1] AS [C2], 
[Project4].[C2] AS [C3]
FROM ( SELECT 
    [Project3].[C1] AS [C1], 
    (SELECT 
        MAX([Extent3].[Quantity]) AS [A1]
        FROM [dbo].[AccountQuantities] AS [Extent3]
        WHERE (27777 = [Extent3].[AccountID]) AND ([Project3].[StartDate] = [Extent3].[StartDate])) AS [C2]
    FROM ( SELECT 
        [Distinct1].[StartDate] AS [StartDate], 
        (SELECT TOP (1) 
            [Extent2].[StartDate] AS [StartDate]
            FROM [dbo].[AccountQuantities] AS [Extent2]
            WHERE (27777 = [Extent2].[AccountID]) AND ([Distinct1].[StartDate] = [Extent2].[StartDate])) AS [C1]
        FROM ( SELECT DISTINCT 
            [Extent1].[StartDate] AS [StartDate]
            FROM [dbo].[AccountQuantities] AS [Extent1]
            WHERE 27777 = [Extent1].[AccountID]
        )  AS [Distinct1]
    )  AS [Project3]
)  AS [Project4]

How can I get it to execute this instead?

SELECT
AccountQuantities.StartDate,
MAX(AccountQuantities.Quantity)
FROM AccountQuantities
WHERE AccountID=27777
GROUP BY StartDate

Again, note the lack of ANY GROUP BY in what's executed. I'm fine with EF optimizing things non-ideally but this is orders of magnitude slower, I cannot find any way to convince it to really do a GROUP BY, and is a major problem for us!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

棒棒糖 2024-11-01 10:00:37

使用 groups.Key 而不是 groups.FirstOrDefault().StartDate

var foo = (from x in Context.AccountQuantities
           where x.AccountID == 27777
           group x by x.StartDate
           into groups
           select new
           {
             groups.Key,
             Quantity = groups.Max(y => y.Quantity)
            }).ToList();

Use groups.Key instead of groups.FirstOrDefault().StartDate

var foo = (from x in Context.AccountQuantities
           where x.AccountID == 27777
           group x by x.StartDate
           into groups
           select new
           {
             groups.Key,
             Quantity = groups.Max(y => y.Quantity)
            }).ToList();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文