Why is DbContext.SaveChanges 10x slower in debug mode

为什么dbcontext.savechanges 10倍的慢,在调试模式

问题 (Question)

Can somebody explain

  1. Why does DbContext.SaveChanges run ~10x slower in debug mode than production mode?
  2. Is there any way I can speed this up?

In debug mode, my webpage takes 116 seconds to load versus 15 seconds if I start the project without debugging.

I have set trace statements and identified that ~100 of the 116 seconds is spent in my DbContext.SaveChanges method when in debug mode.

Running the project without debugging only 7 seconds in spent in the same section.

Let me know in the comments if you'd like more information..

Project Setup:

  • ASP.NET webpage
  • VS2012
  • SQLServer2012
  • Entity Framework 5.0

Additional Info: (Let me know in the comments if you need more)

  • The cumulative number of sql queries over the SaveChanges method is 20,000
  • Production Connection String: Data Source=PC-DEV;Initial Catalog=aspnet-2013-06-04;Integrated Security=True;MultipleActiveResultSets=True;Application Name=EntityFrameworkMUE
  • Debug Connection String: Data Source=PC-DEV;Initial Catalog=aspnet-2013-06-04;Integrated Security=True;MultipleActiveResultSets=True;Application Name=EntityFrameworkMUE
  • I've also experienced the same relative performance with LocalDB as the backing database

Update:

As @ruionwriting suggested, I profiled the database and what I found is that the ~20,000 sql commands take exactly the same time whether the project is run in debug or production mode. ( 0 ms per command).

However, the absolute time difference on average between the 20,000 commands is 5ms in debug mode.

Contrasted with production mode, the average time difference over the set of commands is 0.3 ms.

This is the approximate 10x time performance difference and isolates entity framework as what is taking the extra time in debug mode.

Is there a way to configure the debug build such that EntityFramework can be referenced without debugging flags?

And if I were to somehow achieve the performance back through some compiler magic, what would I lose in terms of debugging capabilities? Currently I can't step into entity framework code so I don't think I would miss anything.

Thanks!

有人可以解释

  1. 为什么dbcontext.savechanges运行~ 10倍慢在调试模式下比生产方式?
  2. 有没有什么方法可以快点吗?

在调试模式下,我的网页需要116秒才能加载和15秒如果我开始项目未调试。

我已将跟踪语句和确定,~ 100的116秒都花在我的dbcontext.savechanges方法在调试模式下。

运行该项目没有调试只有7秒的时间花在同一节。

在评论中让我知道如果您想了解更多的信息。

项目设置:

  • ASP.NET网页
  • vs2012
  • sqlserver2012
  • 实体框架5

附加信息:(在评论中让我知道如果你需要更多的)

  • 在SaveChanges方法的SQL查询的累计数是20000
  • 生产数据源连接字符串:= pc-dev;初始目录= aspnet-2013-06-04;综合安全=真实;真实的应用程序名称multipleactiveresultsets =;= entityframeworkmue
  • 调试数据源连接字符串:= pc-dev;初始目录= aspnet-2013-06-04;综合安全=真实;真实的应用程序名称multipleactiveresultsets =;= entityframeworkmue
  • 我也经历过同样的性能相对LocalDB作为后台数据库

更新:

“我ruionwriting建议,异型数据库,我发现,~ 20000 SQL命令采取完全相同的时间项目是否运行在调试和生产模式。(0毫秒/命令)。

然而,绝对的时间差平均20000之间的命令是5ms在调试模式。

随着生产方式的对比,在命令集的平均时间差为0.3毫秒。

这是近似的10倍的时间性能差异和分离的实体框架所花费额外的时间在调试模式。

有一种方法来配置调试版本,不需要调试EntityFramework旗帜?

如果我能达到的性能通过一些编译器魔法,那么我在调试方面失去?目前我不能进入实体框架代码,所以我不认为我会错过什么。

谢谢!

最佳答案 (Best Answer)

Whohoo!

Ok, so the reason debug mode was exceptionally slow was because Visual Studio's Intellitrace was recording each ADO.NET event ( all 20, 000 of them ) generated by Entity Framework.

So Tools-> Options -> IntelliTrace and Uncheck "Enable IntelliTrace" fixed the issue.

Or one can also just filter out the ADO.NET events by going to Tools->Options -> IntelliTrace -> IntelliTrace Events and uncheck ADO.NET

Thanks for everyone's suggestions.

A section here talks about Will Intellitrace slow down my app

How to Filter IntelliTrace Events

whohoo!

好的,所以调试模式非常慢是因为Visual Studio的IntelliTrace是记录每个ADO.NET事件(20,000)的实体框架生成。

Options -> IntelliTrace and">工具->选项-> IntelliTrace和取消选中“启用IntelliTrace”固定的问题。

或者也可以NET事件过滤Options -> IntelliTrace -> IntelliTrace Events and uncheck ADO.NET">在“工具->选项-> IntelliTrace -> IntelliTrace事件和取消ADO.NET

每个人的建议,谢谢。

这一部分讨论的将IntelliTrace减慢我的应用程序

如何过滤器IntelliTrace事件

答案 (Answer) 2

There are multiple performance considerations for EF and it's known that, compared to many others orm's, operations can be slower than expected/desired for an orm.

(1st) when running on debug will always be slower and (2nd) first run after building will always be slower.

All of this may depend on the complexity of your model. Try to capture the T-SQL statements using SQL Profiler.

有多个性能方面的考虑EF,众所周知,比较许多其他的ORM的,操作可以比预期的要慢/所需的ORM。

(第一)运行时调试会慢,(第二)建设后第一次运行会慢。

所有这一切都取决于你的模型的复杂性。尝试使用SQL事件探查器捕获的T-SQL语句

本文翻译自StackoverFlow,英语好的童鞋可直接参考原文:http://stackoverflow.com/questions/16925972