我有一个应用程序可以根据价格进行大量计算,目前需要约87秒。基于时间表,我怀疑我使用我的包含的方式对于实体框架非常不理想,并且我可以做出重大改进。
我的时间表是这样的:
查询的设计大多是这样的
var result = await db.MyAuthorizedFooExtension()
.Include(x => x.Foofie)
.Include(x => x.Bar.Baz)
.Include(x => x.FooFoo.Select(y => y.BarBar))
.Include(x => x.FooFoo.Select(y => y.BazBaz))
//etc.
.ToListAsync()
结果如下:
[UnionAll7].[C9] AS [C55],
...
FROM (SELECT
CASE WHEN ([Join2].[ID1] IS NULL) THEN CAST(NULL AS int) ELSE 1 END AS [C1],
[Extent1].[ID] AS [ID],
...
[Join2].[ID1] AS [ID5],
...
CAST(NULL AS varchar(1)) AS [C4],
...
FROM [dbo].[Contract] AS [Extent1]
INNER JOIN [dbo].[Leverancier] AS [Extent2] ON [Extent1].[Leverancier] = [Extent2].[ID]
LEFT OUTER JOIN (SELECT [Extent3].[ID] AS [ID1], [Extent3].[Contract] AS [Contract], [Extent3].[ContractStatusOud] AS [ContractStatusOud], [Extent3].[ContractStatusNieuw] AS [ContractStatusNieuw], [Extent3].[MutatieDatumTijd] AS [MutatieDatumTijd], [Extent3].[Medewerker] AS [Medewerker], [Extent3].[Status] AS [Status1], [Extent3].[LastModifiedDate] AS [LastModifiedDate1], [Extent3].[LastModifiedBy] AS [LastModifiedBy1], [Extent4].[ID] AS [ID2], [Extent4].[Naam] AS [Naam], [Extent4].[WindowsNaam] AS [WindowsNaam], [Extent4].[Rol] AS [Rol], [Extent4].[Unit] AS [Unit], [Extent4].[Status] AS [Status2], [Extent4].[LastModifiedDate] AS [LastModifiedDate2], [Extent4].[LastModifiedBy] AS [LastModifiedBy2]
FROM [dbo].[ContractWijzigingHistorie] AS [Extent3]
INNER JOIN [dbo].[Medewerker] AS [Extent4] ON [Extent3].[Medewerker] = [Extent4].[ID] ) AS [Join2] ON [Extent1].[ID] = [Join2].[Contract]
WHERE ( EXISTS (SELECT
1 AS [C1]
FROM [dbo].[Contract] AS [Extent5]
WHERE ( EXISTS (SELECT
1 AS [C1]
FROM [dbo].[Leverancier] AS [Extent6]
WHERE [Extent6].[ID] = [Extent5].[Leverancier]
)) AND ([Extent5].[ID] = [Extent1].[ID])
)) AND ([Extent1].[Status] IN (1)) AND ((DATEPART (year, [Extent1].[Startdatum])) <= @p__linq__0) AND ((DATEPART (year, [Extent1].[Einddatum])) >= @p__linq__1)
UNION ALL
SELECT
2 AS [C1],
[Extent7].[ID] AS [ID],
...
CAST(NULL AS int) AS [C9],
CAST(NULL AS datetime2) AS [C10],
...
[Extent9].[PeriodeWaarde] AS [PeriodeWaarde],
...
[Project9].[LastModifiedBy] AS [LastModifiedBy],
...
[UnionAll5].[ID2] AS [C60],
...
CAST(NULL AS int) AS [C62],
...
etc.
如何在仍使用Entity Framework的同时提高性能?
答案 0 :(得分:0)
在得到查询结果后,这会引发你在做什么的问题?如果您宣读了您的查询,您将看到您在热切地加入相关表格的同时获得了所有结果。
如果在从查询中检索数据后过滤掉这些数据,那么您应该在实现数据之前过滤数据(调用ToList)。你还需要打电话给相关的桌子吗?您还应该只从数据库中选择您将使用的列。
现在,再次了解您希望自己的方法做什么会更容易。创建一个新对象或匿名对象以选择:
var result = await db.MyAuthorizedFooExtension()
//etc.
.Select(s => new
{
Id = s.Id,
FooName = s.FooName,
BazId = s.Bar.Baz.Id
}
.ToListAsync();
最后实现。如果在可选方案中需要包含,请使查询成为IQueryable,最后执行逻辑并实现。