Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework initialization is SLOW -- what can I do to bootstrap it faster?

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).

enter image description here

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

like image 517
Scott Stafford Avatar asked May 25 '12 15:05

Scott Stafford


People also ask

Why is Entity Framework so slow?

Entity Framework loads very slowly the first time because the first query EF compiles the model. If you are using EF 6.2, you can use a Model Cache which loads a prebuilt edmx when using code first; instead, EF generates it on startup.


1 Answers

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/

like image 157
Pawel Avatar answered Sep 22 '22 05:09

Pawel