linq to sql join优化

   初始        时间:常超时
---------------------------  
    var innerJoinQuery = from ca in context.caccey
                                 join cls in context.cclass on ca.caccey_cclassid equals cls.cclass_cclassID
                                 join stl in context.cstyle on ca.caccey_cstyleid equals stl.cstyle_cstyleID
                                 join loc in context.Custom_Captions on ca.caccey_location equals loc.Capt_Code
                                 join suloc in context.subloc on ca.caccey_sublocid equals suloc.subloc_sublocID
                                 where string.Compare(ca.caccey_Name, barCode, true) == 0
                                 && string.Compare(loc.Capt_FamilyType, "Choices", true) == 0 && string.Compare(loc.Capt_Family, "lists_location", true) == 0
                                 select new Garnishry
                                 {
                                     Id = ca.caccey_cacceyID,
                                     Name = ca.caccey_Name,
                                     ItemNo = ca.caccey_itemno,
                                     MaterialId = ca.caccey_cqualyid,
                                     Stone = ca.caccey_stone,
                                     ClassId = ca.caccey_cclassid,
                                     StyleId = ca.caccey_cstyleid,
                                     LocationId = ca.caccey_location,
                                     Location = new StocktakingLocation
                                     {
                                         AreaCode = loc.Capt_Code,
                                         AreaName = loc.Capt_CS
                                     },
                                     SubLocationId = ca.caccey_sublocid,
                                     SubLocation = new SubLocation
                                     {
                                         Id = suloc.subloc_sublocID,
                                         SubLocationName = suloc.subloc_Name.Trim()
                                     },
                                     ItemName = ca.caccey_itemname
                                 };

第一次优化 时间:第1次11~15s ,第2次 3~4s,第3次 1s
----------------------
            var innerJoinQuery = from ca in context.caccey
                                 join cls in context.cclass on ca.caccey_cclassid equals cls.cclass_cclassID
                                 join stl in context.cstyle on ca.caccey_cstyleid equals stl.cstyle_cstyleID
                                 join loc in context.Custom_Captions on ca.caccey_location equals loc.Capt_Code
                                 join suloc in context.subloc on ca.caccey_sublocid equals suloc.subloc_sublocID into caGroup
                                 from suLocItem in caGroup.DefaultIfEmpty()
                                 where string.Compare(ca.caccey_Name, barCode, true) == 0
                                 && string.Compare(loc.Capt_FamilyType, "Choices", true) == 0 && string.Compare(loc.Capt_Family, "lists_location", true) == 0
                                 select new Garnishry
                                 {
                                     Id = ca.caccey_cacceyID,
                                     Name = ca.caccey_Name,
                                     ItemNo = ca.caccey_itemno,
                                     MaterialId = ca.caccey_cqualyid,
                                     Stone = ca.caccey_stone,
                                     ClassId = ca.caccey_cclassid,
                                     Class = cls.cclass_Name,
                                     StyleId = ca.caccey_cstyleid,
                                     Style = stl.cstyle_Name,
                                     LocationId = ca.caccey_location,
                                     Location = new StocktakingLocation
                                     {
                                         AreaCode = loc.Capt_Code,
                                         AreaName = loc.Capt_CS
                                     },
                                     SubLocationId = ca.caccey_sublocid,
                                     SubLocation = ca.caccey_sublocid.HasValue ? new SubLocation
                                     {
                                         Id = suLocItem.subloc_sublocID,
                                         SubLocationName = suLocItem.subloc_Name.Trim()
                                     } : null,
                                     ItemName = ca.caccey_itemname
                                 };

第二次优化 时间:第1次5~8s ,第2次 1~2s,第3次 1s
---------------------------------

            var innerJoinQuery = from ca in context.caccey.Where(ca2 => string.Compare(ca2.caccey_Name, barCode, true) == 0)
                                 join cls in context.cclass on ca.caccey_cclassid equals cls.cclass_cclassID into caCls
                                 from caCls1 in caCls.DefaultIfEmpty()
                                 join stl in context.cstyle on ca.caccey_cstyleid equals stl.cstyle_cstyleID
                                 join loc in context.Custom_Captions.Where(loc2 => string.Compare(loc2.Capt_FamilyType, "Choices", true) == 0 && string.Compare(loc2.Capt_Family, "lists_location", true) == 0)
                                 on ca.caccey_location equals loc.Capt_Code
                                 join suloc in context.subloc on ca.caccey_sublocid equals suloc.subloc_sublocID into caGroup
                                 from suLocItem in caGroup.DefaultIfEmpty()
                                 //where string.Compare(ca.caccey_Name, barCode, true) == 0&&
                                 //string.Compare(loc.Capt_FamilyType, "Choices", true) == 0 && string.Compare(loc.Capt_Family, "lists_location", true) == 0
                                 select new Garnishry
                                 {
                                     Id = ca.caccey_cacceyID,
                                     Name = ca.caccey_Name,
                                     ItemNo = ca.caccey_itemno,
                                     MaterialId = ca.caccey_cqualyid,
                                     Stone = ca.caccey_stone,
                                     ClassId = ca.caccey_cclassid,
                                     Class = caCls1.cclass_Name,
                                     StyleId = ca.caccey_cstyleid,
                                     Style = stl.cstyle_Name,
                                     LocationId = ca.caccey_location,
                                     Location = new StocktakingLocation
                                     {
                                         AreaCode = loc.Capt_Code,
                                         AreaName = loc.Capt_CS
                                     },
                                     SubLocationId = ca.caccey_sublocid,
                                     SubLocation = ca.caccey_sublocid.HasValue ? new SubLocation
                                     {
                                         Id = suLocItem.subloc_sublocID,
                                         SubLocationName = suLocItem.subloc_Name.Trim()
                                     } : null,
                                     ItemName = ca.caccey_itemname
                                 };

第三次优化 时间:第1次3~5s ,第2次 1s
-------------------------------

Garnishry g = (from t in context.caccey
                           where string.Compare(t.caccey_Name, barCode, true) == 0
                           select new Garnishry()
                           {
                               Id = t.caccey_cacceyID,
                               Name = t.caccey_Name,
                               ItemNo = t.caccey_itemno,
                               MaterialId = t.caccey_cqualyid,
                               Stone = t.caccey_stone,
                               ClassId = t.caccey_cclassid,
                               StyleId = t.caccey_cstyleid,
                               LocationId = t.caccey_location,
                               SubLocationId = t.caccey_sublocid,
                               ItemName = t.caccey_itemname
                           }).FirstOrDefault();

            if (g != null)
            {
                var innerJoinQuery = from cls in context.cclass.Where(cls2 => cls2.cclass_cclassID == g.ClassId)
                                     join stl in context.cstyle on g.StyleId equals stl.cstyle_cstyleID
                                     join loc in context.Custom_Captions.Where(loc2 => string.Compare(loc2.Capt_FamilyType, "Choices", true) == 0 && string.Compare(loc2.Capt_Family, "lists_location", true) == 0)
                                     on g.LocationId equals loc.Capt_Code
                                     join suloc in context.subloc on g.SubLocationId equals suloc.subloc_sublocID into caGroup
                                     from suLocItem in caGroup.DefaultIfEmpty()
                                     select new Garnishry
                                     {
                                         Id = g.Id,
                                         Name = g.Name,
                                         ItemNo = g.ItemNo,
                                         MaterialId = g.MaterialId,
                                         Stone = g.Stone,

                                         ClassId = g.ClassId,
                                         Class = cls.cclass_Name,

                                         StyleId = g.StyleId,
                                         Style = stl.cstyle_Name,

                                         LocationId = g.LocationId,
                                         Location = new StocktakingLocation
                                         {
                                             AreaCode = loc.Capt_Code,
                                             AreaName = loc.Capt_CS
                                         },

                                         SubLocationId = g.SubLocationId,
                                         SubLocation = g.SubLocationId.HasValue ? new SubLocation
                                         {
                                             Id = suLocItem.subloc_sublocID,
                                             SubLocationName = suLocItem.subloc_Name.Trim()
                                         } : null,
                                         ItemName = g.ItemName
                                     };
                g = innerJoinQuery.FirstOrDefault();
            }
第四次优化  时间:第1次 1s
---------------
在数据库中对相关字段创建索引。尤其是数据量大的数据表。


结论:
-----------------
前几次优化,不如直接到数据库建索引优化。

原文地址:https://www.cnblogs.com/jes_shaw/p/1558204.html