2013-12-23 110 views
0

我正在開發無線應用程序,當我從SQL Server 2008查詢2000多行時,發生超時異常。SQL Server 2008超時異常

我在循環中使用了很多linq程序。當我評論其中的一些時,它會更好,但仍然會發生。

Linq是否會導致性能下降?是否有其他原因導致此問題?我該如何解決它?

謝謝。

enter code here 
        var listShipUnit = from ShipUnit su in bdd.ShipUnit 
             where (su.ShipmentID == shipmentNumber 
             && (su.QStatus != "1" || su.QStatus == null) 
             && (!su.CaseID.Equals(null) 
             && !su.CaseID.ToUpper().Equals("UNKNOWN") && 
             !su.CaseID.Equals(string.Empty) && 
             !su.CaseID.ToUpper().Equals("NULL") 
             && su.CaseID.Length > 0)) 
              group su by su.CaseID into gsu 
              select new 
              { 
               gsu.Key, 
               gsu, 
               TotalWeight = gsu.Sum(w => w.Weight), 
               TotalVolume = gsu.Sum(v => v.Volume), 
               TotalCount = gsu.Sum(v => v.ShipUnitCount) 
              }; 



        foreach (var shipUnit in listShipUnit) 
        { 
         tempListShipUnitGroup.Add(new ShipUnitGroupSTL(shipUnit.gsu.ToList(), shipUnit.Key, shipUnit.TotalVolume.ToString(), shipUnit.TotalWeight.ToString(), shipUnit.TotalCount.ToString())); 
        } 

        List<ShipUnitGroupSTL> revShipUnitGroup = new List<ShipUnitGroupSTL>(); 
        List<string> finishPallet = new List<string>(); 
        List<string> finishCase = new List<string>(); 


        // 2013.12.19 device null exception ---> start 
        var stlunits = bdd.ScanToLoad; 
        List<ScanToLoad> stlList = stlunits.ToList(); 

        //var shipunits = bdd.ShipUnit; 
        //List<ShipUnit> shipunitList = shipunits.ToList(); 

        if (stlList != null && stlList.Count() > 0) 
        // 2013.12.19 device null exception --- end 
        { 
         //// get the recently record from STL talbe 
         foreach (var shipUnitGroup in tempListShipUnitGroup) 
         { 
          string tempCaseID; 
          string tempPalletID; 

          var unit = shipUnitGroup.GroupShipUnit; 


          tempCaseID = shipUnitGroup.GroupKey; 
          tempPalletID = unit.FirstOrDefault().PalletID; 

         // // 2013.12.19 device null exception ---> start 
         // // look up the caseId in the STL table 
          var stlunit = stlList 
             .Where(s => s.CaseID.Equals(tempCaseID)) 
             .OrderByDescending(s => s.UpdateDate); 

         // //var stlunit = bdd.ScanToLoad 
         // //   .Where(s => s.ScanToLoadID.Equals(tempCaseID) 
         // //    || s.CaseID.Equals(tempCaseID)) 
         // //   .OrderByDescending(s => s.UpdateDate); 

         // // 2013.12.19 device null exception ---> end 



          if (stlunit != null && stlunit.Count() > 0) 
          { 
           string stlPallet = null; 

           stlPallet = stlunit.FirstOrDefault().NewPalletID; 

           if (!string.IsNullOrEmpty(stlPallet)) 
           { 
            if (tempPalletID.Equals(stlPallet)) 
            { 
             finalListShipUnitGroup.Add(shipUnitGroup); 
             //continue; 
            } 
            else 
            { 
             var revShipUnit = bdd.ShipUnit 
                .Where(su => su.ShipmentID == shipmentNumber 
                 && (su.QStatus != "1" || su.QStatus == null) 
                 && su.PalletID.Equals(stlPallet)); 

             if (revShipUnit != null && revShipUnit.Count() > 0) 
             { 
              unit.FirstOrDefault().PalletID = stlPallet; 
              finalListShipUnitGroup.Add(shipUnitGroup); 
             } 
            } 
           } 
           else 
           { 
            finalListShipUnitGroup.Add(shipUnitGroup); 
            //continue; 
           } 


          } 
          else 
          { 
           finalListShipUnitGroup.Add(shipUnitGroup); 
           //continue; 
          } 

          finishCase.Add(tempCaseID); 
          var checkFinshPallet = finishPallet 
                .Contains(tempPalletID); 

          if (checkFinshPallet) 
          { 
           continue; 
          } 

          finishPallet.Add(tempPalletID); 

         } 


         foreach (var tempPalletID in finishPallet) 
         { 
          // look up the pallet in the STL table, add others new caseId in STL table 
          var stlunitPal = stlList 
             .Where(s => s.NewPalletID.Equals(tempPalletID)) 
             .OrderByDescending(s => s.UpdateDate); 

          if (stlunitPal != null && stlunitPal.Count() > 0) 
          { 
           //IEnumerable<MobilePlusServer.ShipUnit> newCaseList; 
           string stlPalletID; 

           // add and remove case from the Pallet 
           foreach (var s in stlunitPal) 
           { 
            stlPalletID = s.NewPalletID; 

            //var addSus = from ShipUnit su in bdd.ShipUnit 
            //    where (su.CaseID.Equals(s.CaseID) && su.RecordID.Equals(s.CaseRecordID)) 
            //     && (su.QStatus != "1" || su.QStatus == null) 
            //    group su by su.CaseID into gsu 
            //    select new 
            //    { 
            //     gsu.Key, 
            //     gsu, 
            //     TotalWeight = gsu.Sum(w => w.Weight), 
            //     TotalVolume = gsu.Sum(v => v.Volume), 
            //     TotalCount = gsu.Sum(v => v.ShipUnitCount) 
            //    }; 

            var addSus = from ShipUnit su in bdd.ShipUnit 
               where (su.CaseID.Equals(s.CaseID) && su.RecordID.Equals(s.CaseRecordID) 
                && (su.QStatus != "1" || su.QStatus == null)) 
               select su; 


            if (addSus != null && addSus.Count() > 0) 
            { 
             var addSu = addSus.FirstOrDefault(); 

             // get the remove pallet id 
             string adShipmentID = addSu.ShipmentID; 
             string adPalletID = addSu.PalletID; 
             //string adRecordID = unit.FirstOrDefault().RecordID.ToString(); 
             //string adCaseID = unit.FirstOrDefault().RecordID.ToString(); 
             if (!shipmentNumber.Equals(adShipmentID) 
              && !finishCase.Contains(s.CaseID)) 
             { 
              // set new pallet id 
              addSu.PalletID = stlPalletID; 

              finalListShipUnitGroup.Add(new ShipUnitGroupSTL(
               addSus.ToList(), 
               s.CaseID, 
               addSu.Volume.ToString(), 
               addSu.Weight.ToString(), 
               addSu.ShipUnitCount.ToString())); 
             } 
            } 
           } 
          } 
         } 
        } 
        // 2013.12.19 device null exception ---> start 
        else 
        { 
         finalListShipUnitGroup = tempListShipUnitGroup; 
        } 
+4

Linq是一個讓事情變慢的層,但這不是你的問題。發佈你正在使用的代碼/查詢,這將是一個更容易回答這個問題。 – paqogomez

+6

**向我們展示一些東西!**我們無法看到您的屏幕,也無法讀懂您的想法 - 您必須**向我們展示**您正在做的事情。你的表格是什麼樣的,你對它們有什麼疑問?你有沒有這些表上的索引 - 如果是的話,**什麼**索引?表格中有多少行,您查詢了多少行? –

+0

@paqogomez linq不應該放慢速度,除非強制查詢評估並進一步查詢(例如'myquery.ToList()。其中​​(x => x.Thing == MatchingThing)')。 – adhocgeek

回答

1

你可以在SQL中使用索引的速度越來越快查詢 我建議在您的加盟領域使用指數

看到這個單證hereherehere

+0

謝謝Pouya。我會嘗試。 – Eric

0

有這樣複雜的查詢,可以很容易地將其解釋爲設計不良的指示(無論是在查詢中還是在數據庫中)。我建議把你的疑問打斷,然後寫出來。這會讓你更清晰的代碼和邏輯。然後,您可以使用SQL server profiler查看執行的確切sql查詢,並查看延遲是否由於Linq to Entities和SQL之間的轉換所致。如果你確定你不能讓你的查詢更簡單,那麼你可以在SQL Server的層面上使用indexing

希望我幫了忙!

+0

謝謝。潘泰利斯。這是非常糟糕的設計,以及許多複雜的Linq。我認爲這是原因。我會嘗試添加索引 – Eric