Kha*_*Ali 3 c# performance entity-framework sql-server-profiler
在通过Web应用程序运行的EF查询与将Profiler生成的T-SQL直接运行到SQL Query窗口之间,我遇到了一些性能评估问题。
以下是通过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
Run Code Online (Sandbox Code Playgroud)
在SQL Profiler中跟踪上述EF查询时,发现执行该操作大约需要221'095 ms。(具有30,000+的应用程序表,具有11,000+的indb_generalInfo和具有30,000+记录的appSancAdvices)。
但是,当我从Profiler复制T-SQL并直接从“查询”窗口运行它时,它只需要大约4000毫秒。
为什么会这样呢?
此查询中的毒液是第一个单词:IEnumerable<application>。如果将其替换为var(即IQueryable),查询将被转换为SQL,直到并包括last Count()。这将花费更少的时间,因为传输的数据量几乎减少为零。
此外,如bobek所述,您不需要Includes,因为您只计算context.applications项目。
除此之外,您将始终注意到使用像实体框架这样的ORM的开销。
| 归档时间: |
|
| 查看次数: |
4352 次 |
| 最近记录: |