我发现使用Web应用程序执行的EF查询和直接在SQL查询窗口中运行Profiler生成的T-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毫秒才能执行完。(应用程序表包含30,000+条记录,indb_generalInfo包含11,000+条记录,appSancAdvices包含30,000+条记录)。
然而,当我从Profiler中复制T-SQL并直接从查询窗口运行它时,仅需要 4'000毫秒。
为什么会这样?
Count()
操作,则无需进行急切加载。 - Simon Belanger