在我的服务中,首先我生成 40,000 种可能的母国和东道国组合,如下所示(clientLocations
包含 200 条记录,因此 200 x 200 是 40,000):
foreach (var homeLocation in clientLocations)
{
foreach (var hostLocation in clientLocations)
{
allLocationCombinations.Add(new AirShipmentRate
{
HomeCountryId = homeLocation.CountryId,
HomeCountry = homeLocation.CountryName,
HostCountryId = hostLocation.CountryId,
HostCountry = hostLocation.CountryName,
HomeLocationId = homeLocation.LocationId,
HomeLocation = homeLocation.LocationName,
HostLocationId = hostLocation.LocationId,
HostLocation = hostLocation.LocationName,
});
}
}
然后,我运行以下查询来查找上述位置的现有费率,但也包括空的缺失费率;生成包含 40,000 行的完整记录集。
var allLocationRates = (from l in allLocationCombinations
join r in Db.PaymentRates_AirShipment
on new { home = l.HomeLocationId, host = l.HostLocationId }
equals new { home = r.HomeLocationId, host = (Guid?)r.HostLocationId }
into matches
from rate in matches.DefaultIfEmpty(new PaymentRates_AirShipment
{
Id = Guid.NewGuid()
})
select new AirShipmentRate
{
Id = rate.Id,
HomeCountry = l.HomeCountry,
HomeCountryId = l.HomeCountryId,
HomeLocation = l.HomeLocation,
HomeLocationId = l.HomeLocationId,
HostCountry = l.HostCountry,
HostCountryId = l.HostCountryId,
HostLocation = l.HostLocation,
HostLocationId = l.HostLocationId,
AssigneeAirShipmentPlusInsurance = rate.AssigneeAirShipmentPlusInsurance,
DependentAirShipmentPlusInsurance = rate.DependentAirShipmentPlusInsurance,
SmallContainerPlusInsurance = rate.SmallContainerPlusInsurance,
LargeContainerPlusInsurance = rate.LargeContainerPlusInsurance,
CurrencyId = rate.RateCurrencyId
});
我尝试过使用 .AsEnumerable()
和 .AsNoTracking()
,这大大加快了速度。 以下代码使我的查询时间缩短了几秒钟:
var allLocationRates = (from l in allLocationCombinations.AsEnumerable()
join r in Db.PaymentRates_AirShipment.AsNoTracking()
但是,我想知道:如何才能加快速度?
编辑:无法在 linq 中复制 foreach
功能。
allLocationCombinations = (from homeLocation in clientLocations
from hostLocation in clientLocations
select new AirShipmentRate
{
HomeCountryId = homeLocation.CountryId,
HomeCountry = homeLocation.CountryName,
HostCountryId = hostLocation.CountryId,
HostCountry = hostLocation.CountryName,
HomeLocationId = homeLocation.LocationId,
HomeLocation = homeLocation.LocationName,
HostLocationId = hostLocation.LocationId,
HostLocation = hostLocation.LocationName
});
我在 from hostLocation in clientLocations
上收到错误,提示“无法将 IEnumerable 类型转换为 Generic.List。”
最佳答案
查询数据库最快的方法是使用数据库引擎本身的功能。
虽然 Linq 是一种非常棒的技术,但它仍然从 Linq 查询中生成一个 select 语句,并针对数据库运行此查询。
最好的选择是创建数据库 View 或存储过程。
View 和存储过程可以轻松集成到 Linq 中。
Material Views(在 MS SQL 中)可以进一步加快执行速度,而缺失索引是迄今为止加速数据库查询的最有效工具。
关于c# - 加速 40,000 行的 linq 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22790302/