【发布时间】:2013-07-04 14:25:21
【问题描述】:
我在通过 Web 应用程序运行 EF 查询和将 Profiler 生成的 T-SQL 直接运行到 SQL 查询窗口之间存在一些性能测量问题。
以下是我通过 Web 应用程序执行的 EF 查询:
IEnumerable<application> _entityList = context.applications
.Include(context.indb_generalInfo.EntitySet.Name)
.Include(context.setup_budget.EntitySet.Name)
.Include(context.setup_committee.EntitySet.Name)
.Include(context.setup_fund.EntitySet.Name)
.Include(context.setup_appStatus.EntitySet.Name)
.Include(context.appSancAdvices.EntitySet.Name)
.Where(e => e.indb_generalInfo != null);
if (isIFL != null)
_entityList = _entityList.Where(e => e.app_isIFL == isIFL);
int _entityCount = _entityList.Count(); // hits the database server at this line
在 SQL Profiler 中跟踪上述 EF 查询时,它显示执行大约需要 221'095 毫秒。 (applications 表有 30,000 多条记录,indb_generalInfo 有 11,000 多条记录,appSancAdvices 有 30,000 多条记录)。
但是,当我从 Profiler 复制 T-SQL 并直接从查询窗口运行它时,只需要大约 4'000 毫秒。
为什么会这样?
【问题讨论】:
-
您是否尝试过多次运行相同的查询? Entity Framework 仍然需要生成该查询,但它将被缓存以供将来的请求使用。此外,如果您正在执行
Count(),则无需预先加载。 -
是的,我之前尝试过。但问题正是@Arnold 通知的。
标签: c# performance entity-framework sql-server-profiler