Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/22.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# EntityFremework:a可以在a之前选择一个优化它的位置吗?_C#_Entity Framework_Linq To Sql_Query Optimization - Fatal编程技术网

C# EntityFremework:a可以在a之前选择一个优化它的位置吗?

C# EntityFremework:a可以在a之前选择一个优化它的位置吗?,c#,entity-framework,linq-to-sql,query-optimization,C#,Entity Framework,Linq To Sql,Query Optimization,我正在尝试提高此查询的性能,我想知道在Where()之前调用Select()是否可以有所改进: public async Task<List<PostValues>> GetValuesToTheDashboard(DataFilter filter, CancellationToken cancellationToken) { long startTimestanp = Helpers.UnixTimeNow(filter.StartD

我正在尝试提高此查询的性能,我想知道在Where()之前调用Select()是否可以有所改进:

 public async Task<List<PostValues>> GetValuesToTheDashboard(DataFilter filter, CancellationToken cancellationToken) {
                long startTimestanp = Helpers.UnixTimeNow(filter.StartDate);
                long endTimestanp = Helpers.UnixTimeNow(filter.EndDate);

                return await
                    _context.CatchDetails.Where(
                        x => x.Monitoring.Client.Id == filter.CustomerId && x.Data.published >= startTimestanp
                             && x.Data.published <= endTimestanp
                             && ((filter.Sentiment == Sentiments.ALL) || x.Sentiment_enum == filter.Sentiment)
                             && (filter.MonitoringId == 0 || x.Monitoring.id == filter.MonitoringId)
                             && (filter.KeywordId == 0 || x.Keyword.Id == filter.KeywordId)
                             && (filter.MotiveId == 0 || x.Motive.Id == filter.MotiveId)
                             && (filter.SocialNetwork.Count == 0 || filter.SocialNetwork.Any(s => x.Data.social_media == s))
                             && (filter.Busca == "" || x.Data.content_snippet.Contains(filter.Busca))
                             && (filter.Gender.Count == 0 || filter.Gender.Any(g => x.Data.extra_author_attributes.gender_enum == g)))
                             .Select(s => new PostValues() {
                                 CatchDetailsId=s.Id,
                                 Monitoring=s.Monitoring.name,
                                 Keyword=s.Keyword.Text,
                                 Motive=s.Motive.Name,
                                 Sentiment=s.Sentiment_enum,
                                 Gender=s.Data.extra_author_attributes.gender_enum,
                                 SocialMedia=s.Data.social_media,
                                 Country=s.Data.extra_author_attributes.world_data.country_code,
                                 State=s.Data.extra_author_attributes.world_data.region,
                                 Published=s.Data.published

                             }).ToListAsync(cancellationToken);
            }
public async Task GetValuesToTheDashboard(数据过滤器过滤器过滤器、取消令牌取消令牌){
long startTimestanp=Helpers.UnixTimeNow(filter.StartDate);
long-endTimestanp=Helpers.UnixTimeNow(filter.EndDate);
返回等待
_context.CatchDetails.Where(
x=>x.Monitoring.Client.Id==filter.CustomerId&&x.Data.published>=startTimestanp
&&x.Data.published x.Data.social_media==s)
&&(filter.Busca==“”| | x.Data.content_snippet.Contains(filter.Busca))
&&(filter.Gender.Count==0 | | filter.Gender.Any(g=>x.Data.extra_author_attributes.Gender_enum==g)))
.Select(s=>newpostValues(){
CatchDetailsId=s.Id,
监控=s.Monitoring.name,
关键字=s.Keyword.Text,
动机=s.Motive.Name,
情绪=情绪,
性别=s.Data.extra_author_attributes.Gender_enum,
社交媒体=s.Data.social_媒体,
Country=s.Data.extra\u author\u attributes.world\u Data.Country\u code,
State=s.Data.extra\u author\u attributes.world\u Data.region,
已发布=s.Data.Published
}).ToListSync(取消令牌);
}

可能有一种方法可以提高性能,但不能通过切换
选择
Where
(如Chetan在评论中提到的)

您可以按顺序构建查询,并基于过滤器最终得到一个更简单的查询。这将是这样的:

var query = _context.CatchDetails.Where(
                x => x.Monitoring.Client.Id == filter.CustomerId && x.Data.published >= startTimestanp
                     && x.Data.published <= endTimestanp);
if (filter.Sentiment != Sentiments.ALL) 
{
    query = query.Where(x => x.Sentiment_enum == filter.Sentiment);
}
if (filter.MonitoringId != 0)
{
    query = query.Where(x => x.Monitoring.id == filter.MonitoringId);
}

[...]

return await 
    query.Select(s => new PostValues() {
        [...]
        }).ToListAsync(cancellationToken);
var query=\u context.CatchDetails.Where(
x=>x.Monitoring.Client.Id==filter.CustomerId&&x.Data.published>=startTimestanp
&&x.Data.published x.thousion_enum==filter.thousion);
}
如果(filter.MonitoringId!=0)
{
query=query.Where(x=>x.Monitoring.id==filter.MonitoringId);
}
[...]
返回等待
query.Select(s=>newpostValues(){
[...]
}).ToListSync(取消令牌);

不要忘记,当SQL返回数据时,变量
query
已经在应用程序的内存中。如果有许多结果,它可能引发内存异常


我建议您限制该搜索的日期范围。

选择“在何处之前”不会有任何区别,因为最终查询执行发生在
ToListAsync
发生时。在选择之前和之后,为数据库生成的查询将是相同的。请怀疑这是否会起到任何作用,因为SQL中的
'blah'='blah'
是微不足道的,而且可能会被执行计划优化。@NPras:对于最简单的查询,您是对的;但是如果你能在where子句中去掉一些连接,它可能会简化优化器的树。这些连接将是整个事件的痛点。谢谢你的回答!在执行之前,我还没有意识到我可以用这种方式组合查询。这帮了大忙:D@NPrasEF将带有非常量表达式的查询转换为参数。所以它不是
'blah'='blah
,而是
@pBlah='blah'
。SQL Server将为参数化查询创建一个可重用的计划。这意味着它必须选择一个对参数的每个值都有效的查询计划。