我正在開發無線應用程序,當我從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;
}
Linq是一個讓事情變慢的層,但這不是你的問題。發佈你正在使用的代碼/查詢,這將是一個更容易回答這個問題。 – paqogomez
**向我們展示一些東西!**我們無法看到您的屏幕,也無法讀懂您的想法 - 您必須**向我們展示**您正在做的事情。你的表格是什麼樣的,你對它們有什麼疑問?你有沒有這些表上的索引 - 如果是的話,**什麼**索引?表格中有多少行,您查詢了多少行? –
@paqogomez linq不應該放慢速度,除非強制查詢評估並進一步查詢(例如'myquery.ToList()。其中(x => x.Thing == MatchingThing)')。 – adhocgeek