EntityFremework:能否在某个地方进行优化之前对此进行选择?

时间:2019-02-25 22:51:49

标签: 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.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);
            }

2 个答案:

答案 0 :(得分:1)

也许有一种方法可以提高性能,但是不能通过切换SelectWhere(如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);

答案 1 :(得分:0)

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

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