实体框架初始化是SLOW - 我可以做些什么来快速引导它? [英] Entity Framework initialization is SLOW -- what can I do to bootstrap it faster?
问题描述
我可以做什么来做我的模型不那么痛苦?我可以预先编译这个吗?更好的是,EF团队可以解决这些问题还是开源框架呢?或至少修复 Warapper
的拼写? :)
编辑:触发这个的一个特定的EF调用基本上是 var db = new MyDbContext(); db.Personnel.Where(a => a.Login == login).SingleOrDefault();
。还有一个EF迁移种子()AddOrUpdate有效生成相同的堆栈。更全面的堆栈跟踪可能会给出更多的上下文:富勒堆栈跟踪
编辑:一些相关链接:
- MSDN:性能注意事项(实体框架)(感谢@ AakashM)
- MSDN: EF Power Tools
- SO:的实体框架4.1,框架4-1大数据表-715>
EDIT2:现在他们只是打开源代码的代码,似乎这行:
//过滤1:1外键与这些细胞包装中使用的组相关的关联。
oneToOneForeignKeyAssociationsForThisWrapper =
oneToOneForeignKeyAssociationsForThisWrapper.Where(
it =>(it.AssociationEndMembers.All(endMember => entityTypes.Contains(endMember.GetEntityType()))))
需要一些工作。它可能不需要使用O(n ^ 2)算法,但我还没有仔细观察。
EDIT3:很高兴,它看起来像在EF6工作正在修复此代码: http://entityframework.codeplex.com/discussions/396130
在EF6中,视图生成对于较大的模型而言是慢的。现在解决方案是使用预生成的视图。这样,您可以在设计时生成视图,并在运行时避免此类工作。要下载EF电源工具并选择优化实体数据模型。它将添加一个包含视图的项目的C#文件。不好的一面是,每当你的模型发生变化,你都需要做。注意:要使用该工具生成视图,在运行时生成视图所需的时间大约需要相同的时间(因此有时您需要耐心等待)。以下是关于EF Power Tools的文章,可能会有所帮助: http://blogs.msdn.com/b/adonet/archive/2011/05/18/ef-power-tools-ctp1-released.aspx
编辑
最近我创建了一个更方便使用的不同解决方案(请注意仅适用于EF6) - http://blog.3d-logic.com/2013/12/14/using-pre-generated-views-without-having-to-pre-generate-views-ef6/
My EF 4.3.1 model has 200-odd tables. Initial startup is horrible, several minutes. A DotTrace-captured profile implies some terrible algorithm/scalability choices deep in the framework, as evidenced by the millions of calls to a number of methods down there and the 36 million IEnumerable.Contains() calls. Here is a snippet, this is all triggered by the first query done on the database (future queries don't do this and are fine).
What can I do to my model to make this less painful? Can I precompile this somehow? Better, can the EF team please address these issues or open source the framework so I can? Or at least fix the spelling of Warapper
? :)
EDIT: One specific EF call that triggers this is basically var db = new MyDbContext(); db.Personnel.Where(a => a.Login == login).SingleOrDefault();
. Also an EF Migrations Seed() AddOrUpdate generates effectively the same stack. The fuller stack trace, which may give a little more context, is here: Fuller Stack Trace
EDIT: Some relevant links:
- MSDN: Performance Considerations (Entity Framework) (thanks to @AakashM)
- MSDN: EF Power Tools
- SO: Entity Framework 4.1 for large number of tables (715)
EDIT2: Now that they just open sourced the code, it appears that this line:
//Filter the 1:1 foreign key associations to the ones relating the sets used in these cell wrappers.
oneToOneForeignKeyAssociationsForThisWrapper =
oneToOneForeignKeyAssociationsForThisWrapper.Where(
it => (it.AssociationEndMembers.All(endMember => entityTypes.Contains(endMember.GetEntityType()))));
is the one that needs some work. It's using an O(n^2) algorithm when it probably doesn't have to, but I haven't looked closely yet.
EDIT3: Happily, it looks like work in EF6 is fixing this code: http://entityframework.codeplex.com/discussions/396130
In pre EF6 view generation is known to be slow for bigger models. For now the solution is to use pregenerated views. This way you generate views at design time and are avoiding this work at runtime. To do that download EF power tools and select "Optimize Entity Data Model". It will add a C# file to your project that contains views. The down side is that you will need to do it each time your model changes. Note: to generate views with the tool it will take about the same amount of time it takes to generate views at runtime (so sometimes you need to be patient). Here is a post about EF Power Tools that might be helpful: http://blogs.msdn.com/b/adonet/archive/2011/05/18/ef-power-tools-ctp1-released.aspx
Edit
Recently I created a different solution that is much more convenient to use (note it only works on EF6) - http://blog.3d-logic.com/2013/12/14/using-pre-generated-views-without-having-to-pre-generate-views-ef6/
这篇关于实体框架初始化是SLOW - 我可以做些什么来快速引导它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!