Linq查询的复杂性限制

时间:2014-03-24 08:13:16

标签: c# linq entity-framework linq-to-entities

我是Linq的忠实粉丝,我一直非常享受表达树等的力量。但我发现,每当我试图对我的查询过于聪明时,我在框架中遇到了某种限制:虽然查询可能需要很短的时间才能在数据库上运行(如性能分析器所示),但结果需要 age 才能实现。当发生这种情况时,我知道我太过花哨了,我开始将查询分解成更小,更小的块 - 所以我有一个解决方案,尽管它可能并不总是最优的。

但我想明白:

  • 在实现查询结果方面,Linq框架在边缘推动的是什么?
  • 我在哪里可以阅读有关实现查询结果的机制?
  • 对于应该避免的Linq查询,是否存在某种可测量的复杂性限制?
  • 已知哪些设计模式会导致此问题,哪些模式可以解决此问题?

编辑:根据评论中的要求,这是我在几秒钟内测量在SQL Server上运行的查询示例,但花了将近2分钟来实现。我不会尝试在上下文中解释所有内容;它只是这样你可以查看结构并看到我正在谈论的一个例子:

Expression<Func<Staff, TeacherInfo>> teacherInfo =
    st => new TeacherInfo
    {
        ID = st.ID,
        Name = st.FirstName + " " + st.LastName,
        Email = st.Email,
        Phone = st.TelMobile,
    };

var step1 =
    currentReportCards.AsExpandable()
        .GroupJoin(db.ScholarReportCards,
                             current =>
                             new { current.ScholarID, current.AcademicTerm.AcademicYearID },
                             past => new { past.ScholarID, past.AcademicTerm.AcademicYearID },
                             (current, past) => new
                             {
                                 Current = current,
                                 PastCards =
                                 past.Where(
                                     rc =>
                                     rc.AcademicTerm.StartDate <
                                     current.AcademicTerm.StartDate &&
                                     rc.AcademicTerm.Grade == current.AcademicTerm.Grade &&
                                     rc.AcademicTerm.SchoolID == current.AcademicTerm.SchoolID)
                             });
// This materialization is what takes a long time:
var subjects = step1.SelectMany(x => from key in x.Current.Subjects
                            .Select(s => new { s.Subject.SubjectID, s.Subject.SubjectCategoryID })
                            .Union(x.PastCards.SelectMany(c => c.Subjects)
                                            .Select(
                                                s => new { s.Subject.SubjectID, s.Subject.SubjectCategoryID }))
         join cur in x.Current.Subjects on key equals
             new { cur.Subject.SubjectID, cur.Subject.SubjectCategoryID } into jcur
         from cur in jcur.DefaultIfEmpty()
         join past in x.PastCards.SelectMany(p => p.Subjects) on key equals
             new { past.Subject.SubjectID, past.Subject.SubjectCategoryID } into past
         select new
         {
             x.Current.ScholarID,
             IncludeInContactSection =
                 // ReSharper disable ConstantNullCoalescingCondition
                (bool?)cur.Subject.IncludeInContactSection ?? false,
             IncludeGrades = (bool?)cur.Subject.IncludeGrades ?? true,
             // ReSharper restore ConstantNullCoalescingCondition
             SubjectName =
                cur.Subject.Subject.Name ?? past.FirstOrDefault().Subject.Subject.Name,
             SubjectCategoryName = cur.Subject.SubjectCategory.Description,
             ClassInfo = (from ce in myDb.ClassEnrollments
                             .Where(
                                 ce =>
                                 ce.Class.SubjectID == cur.Subject.SubjectID
                                 && ce.ScholarID == x.Current.ScholarID)
                             .Where(enrollmentExpr)
                             .OrderByDescending(ce => ce.TerminationDate ?? DateTime.Today)
                     let teacher = ce.Class.Teacher
                     let secTeachers = ce.Class.SecondaryTeachers
                     select new
                     {
                         ce.Class.Nickname,
                         Primary = teacherInfo.Invoke(teacher),
                         Secondaries = secTeachers.AsQueryable().AsExpandable()
                            .Select(ti => teacherInfo.Invoke(ti))
                     })
                .FirstOrDefault(),
             Comments = cur.Comments
                 .Select(cc => new
                 {
                     Staff = cc.Staff.FirstName + " "
                                     + cc.Staff.LastName,
                     Comment = cc.CommentTemplate.Text ??
                                         cc.CommentFreeText
                 }),
             // ReSharper disable ConstantNullCoalescingCondition
             DisplayOrder = (byte?)cur.Subject.DisplayOrder ?? (byte)99,
             // ReSharper restore ConstantNullCoalescingCondition
             cur.Percentile,
             cur.Score,
             cur.Symbol,
             cur.MasteryLevel,
             PastScores = past.Select(p => new
                {
                    p.Score,
                    p.Symbol,
                    p.MasteryLevel,
                    p.ScholarReportCard
                     .AcademicTermID
                }),
             Assessments = cur.Assessments
                 .Select(a => new
                 {
                     a.ScholarAssessment.AssessmentID,
                     a.ScholarAssessment.Assessment.Description,
                     a.ScholarAssessment.Assessment.Type.Nickname,
                     a.ScholarAssessment.AssessmentDate,
                     a.ScoreDesc,
                     a.ScorePerc,
                     a.MasteryLevel,
                     a.ScholarAssessment.Assessment.Type.AssessmentFormat,
                     a.ScholarAssessment.PublishedStatus,
                     a.ScholarAssessment.FPScore,
                     a.ScholarAssessment.TotalScore,
                     a.ScholarAssessment.Assessment.Type.ScoreType,
                     a.ScholarAssessment.Assessment.Type.OverrideBelowLabel,
                     a.ScholarAssessment.Assessment.Type.OverrideApproachingLabel,
                     a.ScholarAssessment.Assessment.Type.OverrideMeetingLabel,
                     a.ScholarAssessment.Assessment.Type.OverrideExceedingLabel,
                 })
            })
        .ToList();

1 个答案:

答案 0 :(得分:1)

Linq对某些任务使用延迟执行,例如在迭代IEnumerable<>时,因此所谓的实现包括一些实际的数据提取。

var reportCards = db.ScholarReportCards.Where(cr => ...); // this prepares the query 
foreach (var rc in reportCards) {} // this executes your query and calls the DB

我认为如果您在SQL服务器上跟踪/查询查询,您可能会在“实现”步骤中看到一些查询。这种问题甚至可能因the "Select N+1" problem等反模式而加剧:例如,您的请求中似乎没有包含AcademicTerm个对象;如果您不解决这些问题,将导致选择N + 1,即每ScholarReportCard次调用,将会调用数据库来懒散地解析附加的AcademicTerm

如果我们专注于Linq to DB方面,至少尽量不要:

  • 选择n + 1:Include您需要的相关数据表
  • 选择太多数据:仅包含您选择中所需的列(您需要的表格上的Include