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
    ---------------
    在数据库中对相关字段创建索引。尤其是数据量大的数据表。


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

  • 相关阅读:
    sqlite错误 Abort due to constraint violation column id is not unique id没开启自动增长
    字符串转为日期类型
    XPTable 一行添加数据 如果想添加多行 可以使用for循环
    在逮捕异常的时候 可以获取e.MESSAGE里面的信息 然后判断是什么异常
    C# 加载图片image (C#)Image.FromFile 方法会锁住文件的原因及可能的解决方法
    计算两个时间的前后 时间戳
    用C#语言写的多线程演示程序:两个线程,可以开始,可以暂停,可以恢复,可以清除。
    sqlite插入日期时候 出现18991230 0:00:00
    datagridview绑定dataset的时候 需要这一句
    WinForm 子线程修改主线程(UI线程)Control 【Z】
  • 原文地址:https://www.cnblogs.com/jes_shaw/p/1558204.html
Copyright © 2011-2022 走看看