下面的查询非常慢且效率低下。根据用户提供的参数,可能需要一分钟才能运行。我们显然可以使查询更有效,但我们的问题是,当它运行时,它似乎锁定了数据库。
在我们的客户网站上,当它运行时,我们的应用程序将挂在我们客户网站上的每台电脑上,直到完成为止。如果我们在管理工作室中运行相同的查询,它仍然需要大约一分钟才能运行,但它不会影响任何其他PC终端..即。数据库没有锁定。
有没有人对通过EF发生这种情况有什么想法? (我们正在使用EF6)
编辑:重申 - 我们的问题不是改进查询(我们可以做到这一点)。这就是为什么这个查询在通过实体框架运行时锁定数据库的原因(影响每个实例)我们的应用程序在我们的客户网站上运行),并且在通过管理工作室运行时不会影响任何应用程序。
var details =
(from stock in
entities.StockTransactions.Where(x => x.Deleted == null
&& x.NouTransactionTypeID == NouvemGlobal.TransactionTypeGoodsReceiptId
&& x.IsBox == true)
join attribute in entities.Attributes.Where(x => DbFunctions.TruncateTime(x.GradingDate) >= start &&
DbFunctions.TruncateTime(x.GradingDate) <= end && x.NouDocStatusID != status) on stock.AttributeID equals attribute.AttributeID
join intakeDetail in entities.APGoodsReceiptDetails on stock.MasterTableID equals intakeDetail.APGoodsReceiptDetailID
join intake in entities.APGoodsReceipts on intakeDetail.APGoodsReceiptID equals intake.APGoodsReceiptID
select new StockDetail
{
LoadingStockDetail = true,
Paid = stock.KillPaymentItems.Any(pay => pay.Deleted == null),
StockTransactionID = stock.StockTransactionID,
APGoodsReceiptDetailID = stock.MasterTableID,
AttributeID = attribute.AttributeID,
TransactionWeight = stock.TransactionWeight,
Serial = stock.Serial,
TransactionDate = stock.TransactionDate,
Detained = attribute.Detained,
Condemned = attribute.Condemned,
Eartag = attribute.Eartag,
KillType = attribute.KillType,
NouDocStatusID = attribute.NouDocStatusID,
Breed = attribute.NouBreed,
Grade = attribute.Grade,
DOB = attribute.DOB,
Identigen = attribute.Identigen,
HoldingNumber = attribute.HoldingNumber,
CountryOfOrigin = attribute.CountryOfOrigin,
KillNumber = attribute.KillNumber,
Generic1 = attribute.Generic1,
CarcassNumber = attribute.CarcassNumber,
CarcassSide = attribute.CarcassSide == 1 ? StockDetail.Side.Side1 : StockDetail.Side.Side2,
AgeInMonths = attribute.AgeInMonths,
AgeInDays = attribute.AgeInDays,
Cleanliness = attribute.NouCleanliness,
SequencedDate = attribute.SequencedDate,
Category = attribute.NouCategory,
Sex = attribute.Sex,
GradingDate = attribute.GradingDate,
CustomerID = attribute.Customer,
FarmAssured = attribute.FarmAssured,
Clipped = attribute.Clipped,
Casualty = attribute.Casualty,
Lame = attribute.Lame,
IsFullCarcass = stock.IsBox,
NumberOfMoves = attribute.NoOfMoves,
PreviousResidency = attribute.PreviousResidency,
TotalResidency = attribute.TotalResidency,
CurrentResidency = attribute.CurrentResidency,
DateOfLastMove = attribute.DateOfLastMove,
Imported = attribute.Imported,
PaidWeight = attribute.ColdWeight,
UTM = attribute.AgeInMonths != null && attribute.AgeInMonths <= ApplicationSettings.GraderLabelUOMAge ? 1 : 0,
OTM = attribute.AgeInMonths != null && attribute.AgeInMonths > ApplicationSettings.GraderLabelUOMAge ? 1 : 0,
SupplierID = intake.BPMasterID_Supplier
})
.ToList();