2013-02-10 61 views
1

我已經做了一個查詢來獲取文章的列表,每個文章綁定到一個「標題」, ,所以我檢索標題加相關的文章及其屬性。 的查詢工作,但在其目前的風格是實體框架 - 子查詢的結果,速度問題?

  • 有點凌亂(在我看來)
  • .ToList()方式需要的時間比我期望的那樣。

有沒有人看到速度問題的任何明顯的原因?

 var offerheaders = 
     from o in dbcontext.F_CAB_OFFER_HEADERS 
     where 
     o.OFHD_FK_BUYER == userinfo.orgaTypeSequence 
     && o.OFHD_VALID_FROM <= userinfo.selectedDate 
     && o.OFHD_VALID_TO >= userinfo.selectedDate 
     && o.OFHD_DELETED_YN == 0 
     && o.OFHD_DELETED_BY_OWNER_YN == false 
     && o.OFHD_OFFER_TYPE == userinfo.offerType 
     orderby o.OFHD_NO ascending 
     select o; 

    var offerlist = 
     from ofhd in offerheaders 
     select new { 
      ofhd = new { 
       OfferNo = ofhd.OFHD_NO, 
       OfferSequence = ofhd.OFHD_SEQUENCE, 
       ValidFrom = ofhd.OFHD_VALID_FROM, 
       ValidTo = ofhd.OFHD_VALID_TO, 
       OfferType = ofhd.OFHD_OFFER_TYPE, 
       Maingroup = new { cdmg_seq = ofhd.F_CAB_CD_MAIN_GROUP_TYPE.CDMG_SEQUENCE, Desc = ofhd.F_CAB_CD_MAIN_GROUP_TYPE.CDMG_DESC }, 
       Supplier = new { 
        Name = ofhd.F_CAB_GROWER.F_CAB_ORGANISATION.ORGA_NAME, 
        Pic = ofhd.F_CAB_GROWER.F_CAB_ORGANISATION.ORGA_FK_PICTURE, 
        Seq = ofhd.F_CAB_GROWER.GROW_SEQUENCE 
       }, 
       Caption = ofhd.OFHD_CAPTION, 
       Seperate = ofhd.OFHD_SHOW_SEPARATE_YN, 
       //ofdts = (from ofdt in dbcontext.F_CAB_OFFER_DETAILS.Where(x => x.OFDT_FK_OFFER_HEADER == ofhd.OFHD_SEQUENCE && x.OFDT_NUM_OF_ITEMS > 0 && x.OFDT_LATEST_DELIVERY_DATE_TIME > compareDateTime && x.OFDT_LATEST_ORDER_DATE_TIME > compareDateTime) 
       ofdts = from ofdt in dbcontext.F_CAB_OFFER_DETAILS 
         join props in dbcontext.F_CAB_CAB_PROP on ofdt.OFDT_FK_CAB_CODE equals props.PROP_FK_CABC_SEQ 
         join cabcode in dbcontext.F_CAB_CD_CAB_CODE on ofdt.OFDT_FK_CAB_CODE equals cabcode.CABC_SEQUENCE 
         join cabgroup in dbcontext.F_CAB_CD_CAB_GROUP on cabcode.CABC_FK_CAB_GROUP equals cabgroup.CDGR_SEQUENCE 
         join grouptype in dbcontext.F_CAB_CD_GROUP_TYPE on cabgroup.CDGR_FK_GROUP_TYPE equals grouptype.CDGT_SEQUENCE 
         join maingrouptype in dbcontext.F_CAB_CD_MAIN_GROUP_TYPE on grouptype.CDGT_FK_MAIN_GROUP equals maingrouptype.CDMG_SEQUENCE 
         join caca in dbcontext.F_CAB_CAB_CASK_MATRIX on ofdt.OFDT_FK_CACA_SEQ equals caca.CACA_SEQUENCE 
         join cask in dbcontext.F_CAB_CD_CASK on caca.CACA_FK_CASK equals cask.CDCA_SEQUENCE 
         join vbncode in dbcontext.F_CAB_CAB_VBN_MATRIX on cabcode.CABC_SEQUENCE equals vbncode.CVMA_FK_CAB_CODE 
         join grel in dbcontext.F_CAB_GENERAL_RELATIONS on ofdt.OFDT_FK_GREL_SEQ equals grel.GREL_SEQUENCE into greltable 
          from g_loj in greltable.DefaultIfEmpty() 
         where 
          ofdt.OFDT_FK_OFFER_HEADER == ofhd.OFHD_SEQUENCE 
          && ofdt.OFDT_NUM_OF_ITEMS > 0 
          && props.PROP_FK_CDLA_SEQ == userinfo.lang.CDLA_SEQUENCE 
         orderby props.PROP_CAB_DESC ascending 
         select new { 
          Desc = props.PROP_CAB_DESC, 
          Group = new { cdgr_seq = cabgroup.CDGR_SEQUENCE, Desc = cabgroup.CDGR_DESC }, 
          Grouptype = new { grouptype.CDGT_SEQUENCE, Desc = grouptype.CDGT_DESC }, 
          Properties = new CABProperties { props = props },    
          Price = ofdt.OFDT_ITEM_PRICE, 
          PIC_SEQ = ofdt.OFDT_FK_PICTURE ?? ((cabcode.CABC_FK_PICTURE ?? cabcode.CABC_SEQUENCE)), 
          PIC_URL = ofdt.OFDT_EXT_PICTURE_REF ?? "", 
          Seq = ofdt.OFDT_SEQUENCE, 
          Available = ofdt.OFDT_NUM_OF_ITEMS, 
          CabCode = ofdt.F_CAB_CD_CAB_CODE.CABC_CAB_CODE, 
          VBNCode = vbncode.CVMA_FK_VBN_CODE, 
          Remark = ofdt.OFDT_REMARK, 
          IsSpecial = ofdt.OFDT_SPECIAL_YN, 
          Arrived = inTransit ? ofdt.OFDT_ARRIVAL_DATE < DateTime.Now : true, 
          Cask = new CABCask { cask = cask, caca = caca },            
          Supplier = g_loj == null ? (ofdt.OFDT_SUPPLIER ?? "") : g_loj.GREL_NAME, 
          SupplierWeb = g_loj == null ? "" : g_loj.GREL_WEBSITE_URL, 
          SupplierLogo = g_loj == null ? ofhd.F_CAB_GROWER.F_CAB_ORGANISATION.ORGA_FK_PICTURE : g_loj.GREL_FK_PICT_SEQ, 
          SupplierSeq = g_loj == null ? -1 : g_loj.GREL_SEQUENCE, 
         } 
      } 
     }; 
    userinfo.mainofferlist = offerlist.ToList(); 
+1

對ToList的調用是執行整個查詢的關鍵。 – 2013-02-10 11:51:47

回答

1

丹尼爾·凱利也提到了ToList功能將在執行查詢,因爲這些LinqToEntities查詢在要首先列舉點執行,ToList這是否能夠創建一個列表。

基本上,爲什麼你的查詢需要這麼多時間可以分爲兩個不同的原因的原因:

  1. 你佔用了過多的預測和我禰
  2. (新{ }的部分)的查詢有加盟條款令人難以置信的大量

我會推薦給您的查詢分爲子查詢,並在

分開運行,如第一部分通過打破,你有很多的子查詢這將是更快,更具有可讀性主查詢
... 
select o 

使用

... 
select o).ToList() 

,所以你必須少「混亂」。

最後但並非最不重要的是,您應該爲匿名對象創建映射,並使用投影之外的那些類來加速查詢。