EF6 / Code First:第一次查询超慢,但只在Debug中 [英] EF6/Code First: Super slow during the 1st query, but only in Debug

查看:1710
本文介绍了EF6 / Code First:第一次查询超慢,但只在Debug中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用EF6 rc1与Code First策略,没有预编译视图,问题是:
如果我编译并运行exe应用程序,运行第一个查询需要15秒钟(没关系,因为我'仍然在预先生成的视图上工作)。但是如果我使用Visual Studio 2013 Preview来调试完全相同的应用程序,则在运行第一个查询之前,需要将近2分钟:

  Dim Context = New MyEntities()
Dim Query = From I in Context.Itens''< ---调试花费2分钟在这里
Dim Item = Query.FirstOrDefault()

有没有办法删除这个额外的时间?我在这里做错了吗?



Ps:上下文本身并不复杂,它只有200多个表。



编辑:发现问题是在调试时,EF似乎正在生成忽略预生成的视图。
使用EF的源代码我发现属性:

  IQueryProvider IQueryable.Provider 
{
get
{
return _provider? (_provider = new DbQueryProvider(
GetInternalQueryWithCheck(IQueryable.Provider)。InternalContext,
GetInternalQueryWithCheck(IQueryable.Provider)。ObjectQueryProvider));
}
}

是消耗时间的地方。但这是奇怪的,因为它只需要调试时间。我在这里缺少一些东西?



编辑:找到更多有关该问题的信息:
使用过程监视器(由Sysinternals)发现, 'desenv.exe'进程耗费大量的时间。更具体地说,它的消耗时间与线程退出。它重复线程退出堆栈36次。我不知道这个信息是非常有用的,但是我用堆栈保存了一个.cvs,这是他的身体:[...](编辑:删除'.cvs'的身体,我可以再发贴通过评论,如果有人真的认为它是有用的,但它是混乱和太大。)



编辑:安装VS2013终极和实体框架6 RTM。安装了实体框架电动工具Beta 4,并用它来生成视图。没有什么改变...如果我运行exe它需要20秒,如果我'开始'调试需要120秒。



编辑:创建一个小项目来模拟错误: http://sdrv.ms/16pH9Vm
只需在环境中直接运行项目,直接通过.exe,点击按钮并比较加载时间。

解决方案

这是Lazy中已知的性能问题使用)调试器。我们目前正在修复(目前我们正在研究的方法是删除使用Lazy)。我们希望很快在补丁程序中发布此修复程序。您可以在我们的CodePlex网站上跟踪此问题的进展情况 - http://entityframework.codeplex.com/workitem/1778



有关即将到来的6.0.2补丁版本的更多详细信息,其中包括修复程序在这里 - http://blogs.msdn.com/b/adonet/archive/2013/10/31/ef6-performance-issues.aspx


I'm using EF6 rc1 with Code First strategy, without precompiled views and the problem is: If I compile and run the exe application it takes like 15 seconds to run the first query (that's okay, since I'm still working on the pre-generated views). But if I use Visual Studio 2013 Preview to Debug the exact same application it takes almost 2 minutes BEFORE running the first query:

Dim Context = New MyEntities()
Dim Query = From I in Context.Itens '' <--- The debug takes 2 minutes in here
Dim Item = Query.FirstOrDefault()

Is there a way to remove this extra time? Am I doing something wrong here?

Ps.: The context itself is not complicated, its just full with 200+ tables.

Edit: Found out that the problem is that during debug time the EF appears to be generating the Views ignoring the pre-generated ones. Using the source code from EF I discovered that the property:

IQueryProvider IQueryable.Provider
    {
        get
        {
            return _provider ?? (_provider = new DbQueryProvider(
                                                 GetInternalQueryWithCheck("IQueryable.Provider").InternalContext,
                                                 GetInternalQueryWithCheck("IQueryable.Provider").ObjectQueryProvider));
        }
    }

is where the time is being consumed. But this is strange since it only takes time in debug. Am I missing something here?

Edit: Found more info related to the question: Using the Process Monitor (by Sysinternals) I found out that there its the 'desenv.exe' process that is consuming tons of time. To be more specific its consuming time with an 'Thread Exit'. It repeats the Thread Exit stack 36 times. I don't know if this info is very useful, but I saved a '.cvs' with the stack, here is his body: [...] (edit: removed the '.cvs' body, I can post it again by the comments if someone really think its going to be useful, but it was confusing and too big.)

Edit: Installed VS2013 Ultimate and Entity Framework 6 RTM. Installed the Entity Framework Power Tools Beta 4 and used it to generate the Views. Nothing changed... If I run the exe it takes 20 seconds, if I 'Start' debugging it takes 120 seconds.

Edit: Created a small project to simulate the error: http://sdrv.ms/16pH9Vm Just run the project inside the environment and directly through the .exe, click the button and compare the loading time.

解决方案

This is a known performance issue in Lazy (which EF is using) when the debugger is attached. We are currently working on a fix (the current approach we are looking at is removing the use of Lazy). We hope to ship this fix in a patch release soon. You can track progress of this issue on our CodePlex site - http://entityframework.codeplex.com/workitem/1778.

More details on the coming 6.0.2 patch release that will include a fix are here - http://blogs.msdn.com/b/adonet/archive/2013/10/31/ef6-performance-issues.aspx

这篇关于EF6 / Code First:第一次查询超慢,但只在Debug中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆