Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to improve Entity Framework 4.0 large model startup performance?

On a project with around 350 entities in the EDMX entity model, my team is experiencing lengthly waits when the first query happens and when the first save happens.

When profiling a simple case which runs a few queries and saves, a simple set of steps just to fire a query and save takes minutes.

The first query takes 47% of the overall time just the call that framework method that executes the query.

The first save takes 50% of the overall time just in System.Data.Objects.ObjectContext.SaveChanges.

Are there any good options to improve performance - this can be a drain on development time.

(Once the system hits production, it's annoying at startup but not a problem during the ongoing execution)

like image 523
BrianCooksey Avatar asked Jul 19 '11 20:07

BrianCooksey


2 Answers

When you use context for the first time it generates mapping model defined in metadata. The option is to pregenerate this model and include pregenerated files in your application (but you must regenerate it each time you modify your EDMX).

Such a big model should be probably divided into multiple smaller models. I hardly believe that 350 entities form single domain which cannot be divided.

like image 183
Ladislav Mrnka Avatar answered Oct 05 '22 07:10

Ladislav Mrnka


A single large EDMX will result in a large ObjectContext. everytime you do using(var ctx = new YourObjectContext()) it is going to construct a large object, initialize a lot of collections (probably 350 of them) and it is going to make your database operations CPU intensive. You will certainly hit performance challenges when you get high volume traffic.

I would suggest breaking the large WDMX into smaller EDMX and produce different ObjectContexts. You should put small number of logically grouped entities into one ObjectContext.

like image 25
oazabir Avatar answered Oct 05 '22 07:10

oazabir