我正在使用實體框架和linq做多個左外連接,但它需要3秒鐘才能通過實體框架運行查詢。實體框架遺漏加入永久(性能問題)
我使用了((System.Data.Objects.ObjectQuery)query).ToTraceString();
並在管理工作室測試了sql語句並立即執行。
這裏是linq代碼。
var query = from Inventory in db.INVENTORies
join NewInventory in db.NEW_INVENTORY on Inventory.Barcode equals NewInventory.Barcode into lj_1
from q_NewInventory in lj_1.DefaultIfEmpty()
join Categories in db.Categorys on Inventory.CAT_ID equals Categories.CAT_ID into lj_2
from q_Categories in lj_2.DefaultIfEmpty()
join SCategories in db.StoneCategories on Inventory.TP_ID equals SCategories.TP_ID into lj_3
from q_SubCategory in lj_3.DefaultIfEmpty()
join qSupplier in db.Suppliers on Inventory.SUP_ID equals qSupplier.SUP_ID into lj_4
from q_Supplier in lj_4.DefaultIfEmpty()
join qStatus in db.Statuses on Inventory.ST_ID equals qStatus.ST_ID into lj_5
from q_Status in lj_5.DefaultIfEmpty()
join q_Locations in db.Locations on Inventory.LOC_ID equals q_Locations.LOC_ID into lj_7
from q_locations in lj_7.DefaultIfEmpty()
join q_Stone1 in db.Stones on Inventory.StoneID_1 equals q_Stone1.STONE_ID into lj_s1
from q_stone1 in lj_s1.DefaultIfEmpty()
join q_Stone2 in db.Stones on Inventory.StoneID_2 equals q_Stone2.STONE_ID into lj_s2
from q_stone2 in lj_s2.DefaultIfEmpty()
join q_Stone3 in db.Stones on Inventory.StoneID_3 equals q_Stone3.STONE_ID into lj_s3
from q_stone3 in lj_s3.DefaultIfEmpty()
join q_Stone4 in db.Stones on Inventory.StoneID_4 equals q_Stone4.STONE_ID into lj_s4
from q_stone4 in lj_s4.DefaultIfEmpty()
join q_Stone5 in db.Stones on Inventory.StoneID_5 equals q_Stone5.STONE_ID into lj_s5
from q_stone5 in lj_s5.DefaultIfEmpty()
join q_Stone6 in db.Stones on Inventory.StoneID_6 equals q_Stone6.STONE_ID into lj_s6
from q_stone6 in lj_s6.DefaultIfEmpty()
join q_Stone7 in db.Stones on Inventory.StoneID_7 equals q_Stone7.STONE_ID into lj_s7
from q_stone7 in lj_s7.DefaultIfEmpty()
join q_Stone8 in db.Stones on Inventory.StoneID_8 equals q_Stone8.STONE_ID into lj_s8
from q_stone8 in lj_s8.DefaultIfEmpty()
join qMasterInventory in db.MASTERINVENTORies on q_NewInventory.InvItemNo equals qMasterInventory.INVITEMNO into lj_6
from q_MasterInventory in lj_6.DefaultIfEmpty()
where Inventory.Barcode == _Barcode
select new
{
inv_InvID = Inventory.INV_ID, inv_Barcode = Inventory.Barcode,
inv_catID = Inventory.CAT_ID, inv_SubCatID = Inventory.TP_ID, inv_Price = Inventory.ITEM_PRICE, inv_Cost = Inventory.ITEM_COST,
inv_PricePoint = Inventory.PricePoint, inv_StatusID = Inventory.ST_ID, inv_StID = Inventory.ST_ID, inv_SupID = Inventory.SUP_ID,
inv_LocID = Inventory.LOC_ID, inv_LabSupplier = Inventory.LabSupplier, inv_LabStone1 = Inventory.LabStone1, inv_LabCategory = Inventory.LabCategory,
inv_LabExtra = Inventory.LabExtra, inv_LabMadeIn = Inventory.LabMadeIn, inv_Width = Inventory.ChainThickNess, inv_Size = Inventory.ChainSize,
inv_Stone1 = Inventory.StoneID_1,inv_Stone2 = Inventory.StoneID_2,inv_Stone3 = Inventory.StoneID_3,inv_Stone4 = Inventory.StoneID_4,inv_Stone5 = Inventory.StoneID_5,inv_Stone6 = Inventory.StoneID_6,inv_Stone7 = Inventory.StoneID_7,inv_Stone8 = Inventory.StoneID_8,inv_Stone9 = Inventory.StoneID_9,inv_Stone10 = Inventory.StoneID_10,
stat_Status = q_Status.DESCRIPTION,
cat_Category = q_Categories.DESCRIPTION,
subCat_SubCategory = q_SubCategory.DESCRIPTION,
sup_Supplier = q_Supplier.Name,
loc_Location = q_locations.DESCRIPTION,
mas_SKU = q_MasterInventory.INVITEMNO,
mas_GUID = q_MasterInventory.ItemGUID,
stone1 = q_stone1.DESCRIPTION,
stone2 = q_stone2.DESCRIPTION,
stone3 = q_stone3.DESCRIPTION,
stone4 = q_stone4.DESCRIPTION,
stone5 = q_stone5.DESCRIPTION,
stone6 = q_stone6.DESCRIPTION,
stone7 = q_stone7.DESCRIPTION,
stone8 = q_stone8.DESCRIPTION,
};
更新統計信息,重建索引... – 2012-01-27 00:59:18
看起來像是模型的問題。石頭1-8?!? – Phill 2012-01-27 01:20:46
我認爲花在EF上的時間將這個查詢重寫爲SQL。正如Mitch指出的那樣,這個查詢看起來很糟糕,而由它創建的SQL可能會更糟。可能你應該看看你的模型/映射 - 對性能的改進可能在那裏。 – Pawel 2012-01-27 01:27:11