zoukankan      html  css  js  c++  java
  • 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
    ---------------
    在数据库中对相关字段创建索引。尤其是数据量大的数据表。


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

  • 相关阅读:
    Java运行时数据区
    关于Java中的内存屏障
    Java中对象在内存中的大小、分配等问题
    【java基础】两个日期的比较大小的几种方法。
    报错信息: java.sql.SQLException: 不支持的字符集 (在类路径中添加 orai18n.jar): ZHS16GBK
    linux 中文件按照时间倒序或者升序显示
    maven本地仓库存在为什么还要连接外网下载?
    【奇奇怪怪的代码问题】-springboot前后端时间不一致
    日常问题-使用maven jetty插件启动慢的一些解决方法
    Mybatis 框架下 SQL 注入攻击的 3 种方式
  • 原文地址:https://www.cnblogs.com/jes_shaw/p/1558204.html
Copyright © 2011-2022 走看看