我知道这是一个相当不寻常的问题,但我花了一些时间试图找出从异常结构化数据库中检索数据的最快方法。基本上,数据库看起来像这样:
'旅行' - 漂亮而简单的结构化表格,这里没有问题
'托运' - 每件货物都链接到上面的旅行。这里有趣且烦人的事情是,每个货物在表中有2个记录,一个用于'Sales'值,一个用于'Costs'值 - 这些记录由Char字段('S'或'C'区分) )。
我的查询基本上需要选择日期范围(日期存储在Trip表中)与行中的销售和成本值之间的所有货物。这是我现在的LINQ查询:
var cons = from cS in dc.Consignments.Where(a => a.BreakdownType == 'S')
join cC in dc.Consignments on new { NTConsignment = cS.NTConsignment, LegacyID = cS.LegacyID, TripDate = cS.TripDate, Depot = cS.Depot, TripNumber = cS.TripNumber, BreakdownType = 'C' } equals new { NTConsingment = cC.NTConsignment, LegacyID = cC.LegacyID, TripDate = cC.TripDate, Depot = cC.Depot, TripNumber = cC.TripNumber, BreakdownType = cC.BreakdownType }
join sl in dc.SageAccounts on new { LegacyID = cS.Customer, Customer = true } equals new { LegacyID = sl.LegacyID, Customer = sl.Customer }
join ss in dc.SageAccounts on sl.ParentAccount equals ss.ID
join vt in dc.VehicleTypes on cS.Trip.VehicleType.Trim() equals vt.ID.ToString() into vtg
where cS.Trip.DeliveryDate >= dates.FromDate && cS.DeliveryDate <= dates.ToDate
orderby cS.Depot, cS.TripNumber
select new GMConsignment
{
NTConsignment = cS.NTConsignment,
Trip = cS.Trip,
LegacyID = cS.LegacyID,
Costs = cC.Value ?? 0.00m,
Sales = cS.Value ?? 0.00m,
Margin = (cS.Value ?? 0.00m) - (cC.Value ?? 0.00m),
Customer = cmS.Customer,
SageID = ss.SageID,
CustomerName = ss.ShortName,
FullCustomerName = ss.Name,
Vehicle = cS.Trip.Vehicle ?? "None",
VehicleType = vtg.SingleOrDefault().VehicleTypeDescription ?? "Subcontractor",
VehicleGroup = vtg.FirstOrDefault().VehicleGroup1
};
问题是,上述情况非常缓慢。我已经分析了SQL,并且我没有明显的错误,例如缺少索引。我知道有一百万种可能性,为什么查询速度很慢,而且如果不了解更多信息就很困难,但我只是想知道是否有任何不同的,可能更快的方式来做我在LINQ中所做的事情? / p>
我也试过在'Select'语句中选择'Cost'(即Costs = dc.Costs.Where ... blah blah),但它甚至更慢。