C# 请帮助我优化这个Linq语句

C# 请帮助我优化这个Linq语句,c#,nhibernate,fluent-nhibernate,C#,Nhibernate,Fluent Nhibernate,可以帮助我优化以下LINQ语句。我用NHibernate作为ORM。执行此语句需要一分钟以上的时间。不应该花那么多时间 var inValidIntroductionDates = environment.IntroductionDateInfo.IntroductionDateDetails .OrderByDescending(x => x.ApplicationDate)

可以帮助我优化以下LINQ语句。我用NHibernate作为ORM。执行此语句需要一分钟以上的时间。不应该花那么多时间

 var inValidIntroductionDates = environment.IntroductionDateInfo.IntroductionDateDetails
                                .OrderByDescending(x => x.ApplicationDate)
                                .Where(x => x.VaccineDetail.Id == vaccine.Id &&
                                            x.ViewInfo.Id == viewInfoDetail.ViewInfo.Id &&
                                            x.MasterForecastInfo.Id == scenarioId &&
                                            x.IsIntroductionDateValid == false)
                                .ToList();

谢谢

Where
子句移到
OrderByDescending
之前,以减少参与order by语句的记录数。像

var inValidIntroductionDates = environment.IntroductionDateInfo.IntroductionDateDetails
                               .Where( x => x.VaccineDetail.Id == vaccine.Id && 
                                     x.ViewInfo.Id == viewInfoDetail.ViewInfo.Id && 
                                     x.MasterForecastInfo.Id == scenarioId && 
                                     x.IsIntroductionDateValid == false)
                                .OrderByDescending(x => x.ApplicationDate)
                                .ToList();
你也可以改变

 x.IsIntroductionDateValid == false


但这不会提高性能。只是一个可读性选项

Where
子句移到
OrderByDescending
之前,以减少参与order by语句的记录数。像

var inValidIntroductionDates = environment.IntroductionDateInfo.IntroductionDateDetails
                               .Where( x => x.VaccineDetail.Id == vaccine.Id && 
                                     x.ViewInfo.Id == viewInfoDetail.ViewInfo.Id && 
                                     x.MasterForecastInfo.Id == scenarioId && 
                                     x.IsIntroductionDateValid == false)
                                .OrderByDescending(x => x.ApplicationDate)
                                .ToList();
var inValidIntroductionDates = environment.IntroductionDateInfo.IntroductionDateDetails.Where(
                        x => x.VaccineDetail.Id == vaccine.Id && x.ViewInfo.Id == viewInfoDetail.ViewInfo.Id && x.MasterForecastInfo.Id == scenarioId && x.IsIntroductionDateValid == false).OrderByDescending(x => x.ApplicationDate).ToList();
你也可以改变

 x.IsIntroductionDateValid == false

但这不会提高性能。只是一个可读性选项

var inValidIntroductionDates = environment.IntroductionDateInfo.IntroductionDateDetails.Where(
                        x => x.VaccineDetail.Id == vaccine.Id && x.ViewInfo.Id == viewInfoDetail.ViewInfo.Id && x.MasterForecastInfo.Id == scenarioId && x.IsIntroductionDateValid == false).OrderByDescending(x => x.ApplicationDate).ToList();
先找到,然后订购


首先查找,然后订购一些需要考虑的事项:

  • 请在您的数据库中附加一个探查器,并告诉我们 发送到数据库的语句
  • 了解语句执行是否缓慢,或者nHibernate处理是否需要时间
  • 如果是数据库查询:优化语句(例如索引、执行计划等)
  • 如果执行的查询太多:与n+1作战
  • 如果是nHibernate执行:关闭nHibernate日志记录
请告诉我们重点是什么

问候,,
Michael

需要考虑的一些事情:

  • 请在您的数据库中附加一个探查器,并告诉我们 发送到数据库的语句
  • 了解语句执行是否缓慢,或者nHibernate处理是否需要时间
  • 如果是数据库查询:优化语句(例如索引、执行计划等)
  • 如果执行的查询太多:与n+1作战
  • 如果是nHibernate执行:关闭nHibernate日志记录
请告诉我们重点是什么

问候,,
迈克尔

对不起!!不走运,也要花同样的时间。对不起!!不走运,也要花同样的时间。对不起!!不走运,也要花同样的时间。对不起!!运气不好,它需要相同的时间。请取出从日志生成的SQL,并在SQL数据库上运行查询执行计划。我怀疑有大量行和不正确的索引将从日志中生成SQL并在SQL数据库上运行查询执行计划。我怀疑有大量的行和不正确的索引