【发布时间】:2015-09-14 18:51:33
【问题描述】:
这篇老文章我已经梳理过了: Why is Entity Framework taking 30 seconds to load records when the generated query only takes 1/2 of a second? 但没有成功。
我已经测试了查询:
- 没有延迟加载(不使用 .Include 相关实体)和
- 没有合并跟踪(使用 AsNoTracking)
由于查询的复杂性和使用 Code First 模型,我认为我一般不能轻易切换到已编译的查询,但如果您遇到其他情况,请告诉我...
设置
- 实体框架“4.4”(安装 EF 5 的 .Net 4.0)
- Code First 模型和 DbContext
- 直接在托管数据库的 SQL Server 2008 机器上进行测试
查询 - 它只是从一个表中返回简单的字段:
SELECT
[Extent1].[Id] AS [Id],
[Extent1].[Active] AS [Active],
[Extent1].[ChangeUrl] AS [ChangeUrl],
[Extent1].[MatchValueSetId] AS [MatchValueSetId],
[Extent1].[ConfigValueSetId] AS [ConfigValueSetId],
[Extent1].[HashValue] AS [HashValue],
[Extent1].[Creator] AS [Creator],
[Extent1].[CreationDate] AS [CreationDate]
FROM [dbo].[MatchActivations] AS [Extent1]
- MatchActivations 表与其他表有关系,但出于此目的,需要根据需要显式加载相关实体。
结果(来自 SQL Server Profiler)
- 对于 Microsoft SQL Server Management Studio 查询:CPU = 78 毫秒,持续时间 = 587 毫秒。
- 对于 EntityFrameworkMUE:CPU = 31 毫秒,持续时间 = 8216 毫秒!
有谁知道,除了建议使用编译查询之外,在使用实体框架进行这样一个简单的查询时还有什么需要注意的吗?
【问题讨论】:
-
你能发布你原来的 EF 查询或 linq 命令吗?
-
这是第一次加载问题还是模型生成后速度很慢? stackoverflow.com/questions/30423838/…
-
查询返回多少行?
-
原来的EF查询很简单:Records = ConfigUnitOfWork.Db.MatchActivations.AsNoTracking().ToList();其中 MatchActivations 是一个 DbSet
.
标签: entity-framework sql-server-2008 entity-framework-4 ef-code-first database-performance