Enity linq查询耗时太长

时间:2017-03-15 01:29:42

标签: c# entity-framework linq entity-framework-6 entity

我有一个快速运行的查询,大约需要两秒钟才能运行,我做了一个更改,现在无法确定改变是什么让现在需要15秒才能运行。任何想法,我可以做什么?

     var longReturnListt =
         await (from i in db.Inventory_Vehicles
                join l in db.Inventory_Descriptions on new {inv = i.ID, cli = i.ClientID} equals
                new {inv = l.InventoryID, cli = l.ClientID} into descGroup
                from m in descGroup.DefaultIfEmpty()
                join s in db.Settings_Exports on i.ClientID equals s.ClientID into settingGroup
                from sg in settingGroup.DefaultIfEmpty()
                join sl in db.Settings_Lots on new {client = sg.ClientID, lot = sg.LotID} equals
                new {client = sl.ClientID, lot = sl.ID} into lotsGroup
                from lg in lotsGroup.DefaultIfEmpty()
                //join se in db.Settings_ExportSites on new {client = lg.ClientID, lotId = i.LotID, site = siteId } equals new {client = se.ClientID, lotId = se.LotID, site = se.Site} into exportGroup
                //from eg in exportGroup.DefaultIfEmpty()
                join f in db.Inventory_Features on new {inv = i.ID, cli = i.ClientID} equals
                new {inv = f.InventoryID, cli = f.ClientID} into invFeatGroup
                from ifg in invFeatGroup.DefaultIfEmpty()
                join p in db.Inventory_Photos on i.ID equals p.InventoryID into photo
                from photos in photo.DefaultIfEmpty()
                where
                i.ClientID == "999995" && i.Archived != "1" && i.Holding != "1" &&
                i.Status == "A"
                orderby i.ID
                select new { i, m, sg, photoo = photo.ToList(), lg, ifg } into grouped
                group grouped by new { grouped.i.ID, grouped.i.ClientID } into groupedDone

                //group photos by new {photos.InventoryID} into pg
                select new NewType
                {
                  InventoryVehicles = (from pho in groupedDone select pho.i).FirstOrDefault(),
                  InventoryDescriptions = (from pho in groupedDone select pho.m).FirstOrDefault(),
                  SettingsExports = (from pho in groupedDone select pho.sg).FirstOrDefault(),
                  //InventoryPhotos = (from pho in groupedDone select pho.photoo).FirstOrDefault(),
                  SettingsLots = (from pho in groupedDone select pho.lg).FirstOrDefault(),
                  InventoryFeatures = (from pho in groupedDone select pho.ifg).FirstOrDefault(),
                  // SettingsExportSites = (from pho in groupedDone select pho.eg).FirstOrDefault()
                }).ToListAsync();
    returnList.AddRange(longReturnListt);
    return returnList;

0 个答案:

没有答案