我进行了几次搜索,但我仍在为此苦苦挣扎。 我想使用SQL Server Exists()在EF核心上执行此查询:
问题是,在sql server中,下面的查询大约需要2秒钟才能执行。在C#上,使用Any()方法花费的时间超过1分钟。它使 在HutOperationHistory和HutPropertyValue表上的几个查询。
//花费1-2秒
select * From Hut h where exists(
select 1 from HutOperationHistory
where HutId = h.HutId and HutOperationId = 4
)
and exists(
select 1 from HutPropertyValue
where HutId = h.HutId and Value = 'HUT_SUPPLIER_PROPERTY_NAME'
)
and exists(
select 1 from HutPropertyValue
where HutId = h.HutId and Value = 'BUY_ORDER_PROPERTY_NAME'
)
and exists(
select 1 from HutPropertyValue
where HutId = h.HutId
and Value = 'WORK_ORDER_PROPERTY_NAME'
)
我在linq上做到了,它像这样:
// takes more then 1 minute
var huts = coreDbContext.Huts
.Include(x => x.PropertyValuesList).ThenInclude(y => y.HutProperty)
.Include(x => x.OperationHistoriesList)
.Where(
x =>
x.OperationHistoriesList.Any(
z => ( z.HutOperation.hutOperationId = 4 )
)
&& x.PropertyValuesList.Any(b =>
b.Value.Equals(hutSupplierPropName, StringComparison.CurrentCultureIgnoreCase)
)
&& x.PropertyValuesList.Any(b =>
b.Value.Equals(buyOrderPropName, StringComparison.CurrentCultureIgnoreCase)
)
&& x.PropertyValuesList.Any(b =>
b.Value.Equals(workOrderPropName, StringComparison.CurrentCultureIgnoreCase)
)
)
.OrderBy(x => x.Order)
.ToList();
我能做些什么使它更快吗?
提前谢谢!
答案 0 :(得分:0)
尝试使用Count()> 0,而不是Any()。过去,使用此功能可以提高性能