在LINQ to Entities中優化Group By

entity-framework linq linq-to-entities sql

我在LINQ to Entities中有這個查詢。

        var query = (from s in db.ForumStatsSet
                     where s.LogDate >= date1 && s.LogDate <= date2
                     group s by new { s.Topic.topicID, s.Topic.subject, s.Topic.Forum.forumName, s.Topic.datum, s.Topic.Forum.ForumGroup.name, s.Topic.Forum.forumID } into g
                     orderby g.Count() descending
                     select new TopicStatsData
                     {
                         TopicId = g.Key.topicID,
                         Count = g.Count(),
                         Subject = g.Key.subject,
                         ForumGroupName = g.Key.name,
                         ForumName = g.Key.forumName,
                         ForumId = g.Key.forumID
                     });

我知道這是一種“邪惡”查詢,但它僅用於管理界面。但它生成的SQL絕對令人恐怖。看看這個寶貝。


exec sp_executesql N'SELECT TOP (50) 
[Project6].[C1] AS [C1], 
[Project6].[TopicId] AS [TopicId], 
[Project6].[C4] AS [C2], 
[Project6].[subject] AS [subject], 
[Project6].[name] AS [name], 
[Project6].[forumName] AS [forumName], 
[Project6].[C2] AS [C3]
FROM ( SELECT 
    [Project5].[TopicId] AS [TopicId], 
    [Project5].[subject] AS [subject], 
    [Project5].[forumName] AS [forumName], 
    [Project5].[name] AS [name], 
    1 AS [C1], 
     CAST( [Project5].[forumID] AS int) AS [C2], 
    [Project5].[C1] AS [C3], 
    [Project5].[C2] AS [C4]
    FROM ( SELECT 
        [Project4].[TopicId] AS [TopicId], 
        [Project4].[forumID] AS [forumID], 
        [Project4].[subject] AS [subject], 
        [Project4].[forumName] AS [forumName], 
        [Project4].[name] AS [name], 
        [Project4].[C1] AS [C1], 
        (SELECT 
            COUNT(cast(1 as bit)) AS [A1]
            FROM        [dbo].[tForumStats] AS [Extent14]
            LEFT OUTER JOIN [dbo].[tTopic] AS [Extent15] ON [Extent14].[TopicId] = [Extent15].[topicID]
            LEFT OUTER JOIN [dbo].[tForum] AS [Extent16] ON [Extent15].[forumID] = [Extent16].[forumID]
            LEFT OUTER JOIN [dbo].[tForum] AS [Extent17] ON [Extent15].[forumID] = [Extent17].[forumID]
            LEFT OUTER JOIN [dbo].[tForum] AS [Extent18] ON [Extent15].[forumID] = [Extent18].[forumID]
            LEFT OUTER JOIN [dbo].[tForumGroup] AS [Extent19] ON [Extent18].[forumGroupID] = [Extent19].[forumGroupID]
            LEFT OUTER JOIN [dbo].[tForum] AS [Extent20] ON [Extent15].[forumID] = [Extent20].[forumID]
            LEFT OUTER JOIN [dbo].[tForumGroup] AS [Extent21] ON [Extent20].[forumGroupID] = [Extent21].[forumGroupID]
            WHERE ([Extent14].[LogDate] >= @p__linq__25) AND ([Extent14].[LogDate] = @p__linq__25) AND ([Extent6].[LogDate] = @p__linq__25) AND ([Extent1].[LogDate] 

我不像任何人那樣解釋該查詢,但是如果能夠優化查詢以便它只是進行簡單的常規連接,那將會很棒。如果我自己編寫SQL,這樣的東西就可以了。

SELECT COUNT(*) AS NumberOfViews, s.topicid AS topicId, t.subject AS TopicSubject, g.[name] AS ForumGroupName, f.forumName AS ForumName 
FROM tForumStats s
join tTopic t on s.topicid = t.topicid
join tForum f on f.forumid = t.forumid
JOIN tForumGroup g ON f.forumGroupID = g.forumGroupID
WHERE s.[LogDate] between @date1 AND @date2
group by s.topicid,  t.subject, f.Forumname, t.Datum, g.[name]
order by count(*) desc

順便說一句,我喜歡這個網站。驚人的設計和可用性!希望能得到一些幫助:)

一般承認的答案

您可以自己加入給定的表,而不是加入組中的所有表。你能嘗試一下嗎?

from s in db.ForumStatsSet
join t in db.Topics on t.TopicId == s.TopicId
join f in db.Forums on f.ForumId == t.ForumId
join fg in db.ForumGroups on fg.ForumGroupId == f.ForumGroupId
where s.LogDate >= date1 && s.LogDate <= 
group s by new { t.TopicId, t.subject, f.forumName, t.datum, fg.name, f.forumID } into g
orderby g.Count() descending
select new TopicStatsData
{
 TopicId = g.Key.topicID,
 Count = g.Count(),
 Subject = g.Key.subject,
 ForumGroupName = g.Key.name,
 ForumName = g.Key.forumName,
 ForumId = g.Key.forumID
 });

ps:可能有一些錯誤,但邏輯上它應該是正確的!


熱門答案

我認為問題在於分組結構。嘗試首先提取數據(因此不需要通過屬性),然後按提取的數據進行分組。

IOW,嘗試編寫LINQ,就像SQL一樣。



許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
這個KB合法嗎? 是的,了解原因
許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
這個KB合法嗎? 是的,了解原因