2011-08-31 68 views
2

我有一個問題,讓linq到實體執行良好。我有的查詢(不是我的,維護某人的代碼:-))有幾個包括我已經確定的WPF屏幕都需要使用此查詢的結果。Linq到許多列的實體性能問題

現在,生成的SQL執行速度非常快,只返回一行數據。但它正在返回570列,並且我認爲性能受到創造所有對象和所有這些領域的開銷。

我試過使用延遲加載,但似乎沒有任何影響性能。

我嘗試刪除任何不必要的「包含」語句,但似乎它們都是需要的。

這裏的LINQ查詢:

var myQuery = 
       from appt in ctx.Appointments 
           .Include("ScheduleColumnProfile") 
           .Include("EncounterReason") 
           .Include("Visit") 
           .Include("Visit.Patient") 
           .Include("Visit.Patient.PatientInsurances") 
           .Include("Visit.Patient.PatientInsurances.InsuranceType") 
           .Include("Visit.Patient.PatientInsurances.InsuranceCarrier") 
           .Include("MasterLookup") 
           .Include("User1") 
           .Include("User2") 
           .Include("Site") 
           .Include("Visit.Patient_CoPay") 
           .Include("Visit.Patient_CoPay.User") 
           .Include("Visit.VisitInstructions.InstructionSheet") 
        where appt.VisitId == visitId 
         && appt.MasterLookup.LookupDescription.ToUpper() != Rescheduled 
         && appt.Site.PracticeID == practiceId 
         && appt.MasterLookup.LookupDescription.ToUpper() != Cancelled 
        orderby appt.AppointmentId descending 
        select appt; 

的SQL生成是4000線長與選擇statment 570列和3或4聯承滴盤,所以我不打算將其粘貼在這裏,除非有人真的想看看它。基本上,我正在尋找一種可能的方式來擺脫工會,並將列修剪到只需要的部分。

幫助!

:-)

+0

這裏閱讀:http://stackoverflow.com/questions/5521749/how-many-include-i-can-use-on-objectset-in-entityframework-to-retain-performance/5522195#5522195 –

+0

謝謝,這有助於解釋使用的影響。包括。那麼,解決方案是否會使用objectContext.LoadProperty()? – Scot

+0

解決方案是使用多種技術,找到在您的場景中提供最佳性能的解決方案。 –

回答

2

如果有人跟蹤,這是最終爲我工作的解決方案。感謝所有評論和提出建議的人......它最終將我帶到了我的下面。

  ctx.ContextOptions.LazyLoadingEnabled = true; 

      var myQuery = 
       from appt in ctx.Appointments 
       where appt.VisitId == visitId 
        && appt.MasterLookup.LookupDescription.ToUpper() != Rescheduled 
        && appt.Site.PracticeID == practiceId 
        && appt.MasterLookup.LookupDescription.ToUpper() != Cancelled 
       orderby appt.AppointmentId descending 
       select appt; 


      var myAppt = myQuery.FirstOrDefault(); 

      ctx.LoadProperty(myAppt, a => a.EncounterReason); 
      ctx.LoadProperty(myAppt, a => a.ScheduleColumnProfile); 
      ctx.LoadProperty(myAppt, a => a.Visit); 
      ctx.LoadProperty(myAppt, a => a.MasterLookup); 
      ctx.LoadProperty(myAppt, a => a.User1); 
      ctx.LoadProperty(myAppt, a => a.User2); 
      ctx.LoadProperty(myAppt, a => a.PatientReferredProvider); 

      var myVisit = myAppt.Visit; 

      ctx.LoadProperty(myVisit, v => v.Patient); 
      ctx.LoadProperty(myVisit, v => v.Patient_CoPay); 
      ctx.LoadProperty(myVisit, v => v.VisitInstructions); 
      ctx.LoadProperty(myVisit, v => v.EligibilityChecks); 

      var pat = myVisit.Patient; 

      ctx.LoadProperty(pat, p => p.PatientInsurances); 


      //load child insurances 
      foreach (PatientInsurance patIns in myAppt.Visit.Patient.PatientInsurances) 
      { 
       ctx.LoadProperty(patIns, p => p.InsuranceType); 
       ctx.LoadProperty(patIns, p => p.InsuranceCarrier); 
      } 

      //load child instruction sheets 
      foreach (VisitInstruction vi in myAppt.Visit.VisitInstructions) 
      { 
       ctx.LoadProperty(vi, i => i.InstructionSheet); 
      } 

      //load child copays 
      foreach (Patient_CoPay coPay in myAppt.Visit.Patient_CoPay) 
      { 
       ctx.LoadProperty(coPay, c => c.User); 
      } 

      //load child eligibility checks 
      foreach (EligibilityCheck ec in myAppt.Visit.EligibilityChecks) 
      { 
       ctx.LoadProperty(ec, e => ec.MasterLookup); 
       ctx.LoadProperty(ec, e => ec.EligibilityResponse); 
      } 
0

我會建議您製作僅包含您需要顯示屬性的新類。當您投影到新類型時,您不需要包含語句,但仍然可以訪問實體的導航屬性。

var myQuery = from appt in ctx.Appointments 
       where appt.VisitId == visitId 
        && appt.MasterLookup.LookupDescription.ToUpper() != Rescheduled 
        && appt.Site.PracticeID == practiceId 
        && appt.MasterLookup.LookupDescription.ToUpper() != Cancelled 
       orderby appt.AppointmentId descending 
       select new DisplayClass 
       { 
       Property1 = appt.Prop1, 
       Proeprty2 = appt.Visit.Prop1, 
       . 
       . 
       . 
       }; 
+0

還有其他選擇嗎?消耗這個的頁面期待約會和所有關聯的導航屬性。 – Scot

+0

@Scot - 它是否真的檢查它是否是約會實體?如果它只是綁定到某些屬性名稱,則可以使您的DisplayClass實體具有相同的名稱。 – Aducci

+0

是的,它需要約會實體及其關聯的子對象。這是我遇到的問題,與「包含」它需要很長時間來執行,沒有包括我不能訪問任何這些子對象 – Scot