Comparing times for loading models ofdifferent (exaggerated) sizes

Prompted by Roger Jennings' post Entity Framework Instantiation Times with a 120-Table Database, but with limited time to do a thorough investigation, I did a quick trial this morning comparing AdventureWorksLT with it's dozen tables/views to another model with about 400.

There are a few up-front costs for querying in Entity Framework. Query compilation is one and another biggie is loading the model into the application cache. Once the model is loaded, it remains in the applications cache and under normal circumstances, won't need to load again. EF only loads the model pieces as needed. In default scenarios, the conceptual layer is first loaded when ObjectContext is instantiated; the store and mapping layers are loaded the first time a query needs to be compiled.

I am starting the stopwatch prior to the instantiation of each context and stopping when the results have been iterated through completely. I ran the tests numerous times with consistent results.

 First query in applicationSame query in a completely new objectContext, which benefits from the model already being loaded
AdventureWorksLT1512 ms36 ms
400+ entity model4411ms46ms

The entity queried in the second test is much wider (many more properties) than the entity queried in the AdventureWorks test. The effort of materializing the app. 200 large objects understandably great than materializing the app. 400 smaller objects from AdventureWorks, so I'm not concerned abut the 10 ms difference there.

My recommendation is to preload the model before you need to do any queries.  You can manually load a metadataworkspace or use this lame trick (Quick Trick for forcing MetadataWorkspace ItemCollections to load) to force it to load early in your application life-cycle.

I've gotten some interesting comments on yesterday's post about building models from very large databases: Entity Framework Designer and Large Databases

#1 Steve on 8.20.2008 at 11:54 PM

Hi Julie,If I am thinking of using Astoria on top of the entity framework for a Silverlight application will you "quick trick" work with Astoria or will it need to read the metadata for each call that is created by the silverlight client ?

#2 Julia Lerman on 8.21.2008 at 7:07 AM

Hi SteveThe metadata remains in the application cache. A web service is an application and stays in memoroy regardless of how many clients come in and out, so to speak. I would imagine that Astoria, like any other web service, would therefore keep the metadata in memory for it's life time, which is until something stops the application- eg server restart, etc. So it's just the very first query that will trigger the load. I don't think you should need to do this with a web service. just test the service and voila, you have your first metadata load. Different from a client side application which stops and starts all of the time.julie

Leave a Comment