You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Grouping by an entity should not group by all entity columns #2638
var results = (from o in db.Orders
group o by o.Customer into g
select new
{
CustomerID = g.Key.ID,
Count = g.Count(),
}).ToList();
The reason this is being done here is so that we can pick any property from g.Key that we need.
The SQL is a little suboptimal:
SELECT
[GroupBy1].[K1] AS [ID],
[GroupBy1].[A1] AS [C1]
FROM ( SELECT
[Extent2].[ID] AS [K1],
[Extent2].[Name] AS [K2],
[Extent2].[HasFreeShipping] AS [K3],
COUNT(1) AS [A1]
FROM [dbo].[Orders] AS [Extent1]
INNER JOIN [dbo].[Customers] AS [Extent2] ON [Extent1].[CustomerID] = [Extent2].[ID]
GROUP BY [Extent2].[ID], [Extent2].[Name], [Extent2].[HasFreeShipping]
) AS [GroupBy1]
The query groups by all entity columns. This is a problem because it prevents index usage and moves a lot more data through the query pipeline than necessary. If there are big string columns this might increase the required aggregation state size by orders of magnitude, even spill to disk.
The group by columns should be the union of the primary key and all columns actually referenced. It would be nice if EF did this.
I'm aware of a workaround but this query as clean as it is seems preferable to me.
The model for your convenience:
public class Customer
{
public int ID { get; set; }
public string Name { get; set; }
public bool? HasFreeShipping { get; set; }
public virtual IList<Order> Orders { get; set; }
}
public class Order
{
public int ID { get; set; }
public int CustomerID { get; set; }
public Customer Customer { get; set; }
public DateTime CreateDateTime { get; set; }
}
This issue relates to EF6, which is managed in a separate repo (currently on CodePlex, but we are considering moving it to GitHub). We have taken a note on #2341 to consider this scenario and the SQL you suggested when we work on GroupBy translation in EF7.
Regarding this scenario in EF6, give that we are going to improve this in EF7 and the relative complexity/risk of adjusting the query pipeline in EF6 we aren't planning to address this in EF6. We would consider accepting a pull request though if someone outside our team wanted to contribute the change.
Activity
rowanmiller commentedon Jul 24, 2015
Hey,
This issue relates to EF6, which is managed in a separate repo (currently on CodePlex, but we are considering moving it to GitHub). We have taken a note on #2341 to consider this scenario and the SQL you suggested when we work on GroupBy translation in EF7.
Regarding this scenario in EF6, give that we are going to improve this in EF7 and the relative complexity/risk of adjusting the query pipeline in EF6 we aren't planning to address this in EF6. We would consider accepting a pull request though if someone outside our team wanted to contribute the change.
~Rowan