Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 实体框架按多个相关实体计数排序-效率_C#_Sql_Entity Framework_Linq To Entities - Fatal编程技术网

C# 实体框架按多个相关实体计数排序-效率

C# 实体框架按多个相关实体计数排序-效率,c#,sql,entity-framework,linq-to-entities,C#,Sql,Entity Framework,Linq To Entities,所以我有一个主题,有这些相关的实体 - List<Posts> - List<Votes> - List<Views> -列表 -名单 -名单 我有以下疑问。在这里,我想根据特定日期段内3个相关实体的数量提取并订购热门主题 var topics = _context.Topic .OrderByDescending(x => x.Posts.Count(c => c.DateCreated >= from && c.

所以我有一个主题,有这些相关的实体

 - List<Posts>
 - List<Votes>
 - List<Views>
-列表
-名单
-名单
我有以下疑问。在这里,我想根据特定日期段内3个相关实体的数量提取并订购热门主题

var topics = _context.Topic
.OrderByDescending(x => x.Posts.Count(c => c.DateCreated >= from && c.DateCreated <= to))
 .ThenByDescending(x => x.Votes.Count(c => c.DateCreated >= from && c.DateCreated <= to))
.ThenByDescending(x => x.Views.Count(c => c.DateCreated >= from && c.DateCreated <= to))
.Take(amountToShow)
.ToList();
var topics=\u context.Topic

.OrderByDescending(x=>x.Posts.Count(c=>c.DateCreated>=from&&c.DateCreated x.voates.Count(c=>c.DateCreated>=from&&c.DateCreated x.Views.Count)(c=>c.DateCreated>=from&&c.DateCreated如果您将上述代码放入或使用探查器检查它,您将看到它可能会生成类似以下SQL的内容:

SELECT TOP @amountToShow [t0].[id] --and additional columns
FROM [Topic] AS [t0]
ORDER BY (
    SELECT COUNT(*)
    FROM [Posts] AS [t1]
    WHERE ([t1].DateCreated >= @from AND [t1].DateCreated <= @to) 
        AND ([t1].[topidId] = [t0].[id])
    ) DESC, (
    SELECT COUNT(*)
    FROM [Votes] AS [t2]
    WHERE ([t2].DateCreated >= @from AND [t2].DateCreated <= @to) 
        AND ([t2].[topicId] = [t0].[id])
    ) DESC , (
    SELECT COUNT(*)
    FROM [Views] AS [t3]
    WHERE ([t3].DateCreated >= @from AND [t3].DateCreated <= @to) 
        AND ([t3].[topicId] = [t0].[id])
    ) DESC
GO
但是让LINQ生成这样的代码充其量是不确定的。执行
左连接
s并不是它的强项,使用现有的技术可能会生成使用
交叉应用
和/或
外部应用
的SQL,并且可能与当前代码一样慢或更慢

如果你担心速度,你可以考虑把你的微调SQL放进一个视图中,这样你就知道所使用的查询是你想要的。


也请记住,您或其他人以后必须返回此代码并对其进行维护。您当前的linq语句非常简单易懂。复杂的查询将更难维护,将来需要进行更多的修改。

我不知道是否有人能回答此问题。我不想重复我无法用实际数据(更不用说索引的影响)对此进行基准测试,但我可以给出一个答案。可能只是获取计数并在内存中进行排序(即客户端)更快。如果性能对您很重要,我会预先计算主题的受欢迎程度,以便您可以轻松检索此信息。当然,如果您需要实时数据,此解决方案不适用,但在我看来,对于此类问题,您不需要100%的实时数据。这一切取决于您数据的特征。有多少记录?什么索引?安德烈提到,在检查您的样本时,这看起来像是缓存/预煅烧的主要用例示例。
SELECT TOP @amountToShow [t0].[id] --etc
FROM [Topic] AS [t0]
LEFT JOIN 
    (SELECT topicId, COUNT(*) AS num FROM Posts p 
    WHERE [p].DateCreated >= @from AND .DateCreated <= @to 
    GROUP BY topicId) [t1]
ON t0.id = t1.topicId
LEFT JOIN 
    (SELECT topicId, COUNT(*) AS num FROM Votes vo 
    WHERE [vo].DateCreated >= @from AND [vo].DateCreated <= @to 
    GROUP BY topidId) [t2]
ON t0.id = t2.topicId
LEFT JOIN 
    (SELECT topicId, COUNT(*) AS num FROM Views vi 
    WHERE [vi].DateCreated >= @from AND [vi].DateCreated <= @to 
    GROUP BY topicId) [t3]
ON t0.id = t3.topicId
ORDER BY t1.num DESC, t2.num DESC, t3.num DESC