Reputation: 595
I'm trying to increase the "first call" execution time for my EF queries and found the possibility to use the precompiled views for the queries. After I had generated the precompiled views using the T4 template from VS gallery called "EF Code First Pre-generated views generator for C#", I haven't noticed any performance boost for some of my heavy queries (with Includes and Joins).
Then I tried to investigate the generated code by the t4 template. I've seen there a class descending from the DbMappingViewCache
, which returns the requested DbMappingView
through its method GetView(EntitySetBase extent)
.
It looks like all these views are only for the simple queries, so I ask myself if there is any way to cache the view for my specific heavy query on the precompilation stage. Does anyone have an idea how to achieve that? Is it possible at all?
Upvotes: 4
Views: 2830
Reputation: 76218
Take a look at this blog post which talks about few ways to cut down startup time.
For reference:
Using a cached db model store
This has probabily the biggest impact on startup performance and is only necessary if you are using the code first model. Building and compiling large models using the Code First pipeline is extremly expensive in terms of start up time. This step will cache the code-first pipeline with its expensive o-c mapping generation and will store it in a xml file on the filesystem. The next time your application starts, EF will deserialize this cached mapping file which significantly reduces startup time.
Generate pre-compiled views:
You're already doing it but also take a look at Interactive Pre Generated Views for Entity Framework 6 which let's you precompile views and cache them without getting bogged down with increased build time.
Generate pre-compiled version of entityframework using n-gen to avoid jitting
Entity Framework does not come in the default installation of the .net Framework. Therefore, the EF assembly is not NGEN'd by default which means that EF code needs to be JITTED each time the application starts. Since EF is a really large and complex framework (EntityFramework assembly has over 5MB), and most of the code paths are needed even for simple scenarios, JITTING has a noticeable impact on startup performance.
Running NGEN against EF is as simple as executing the following command within a root terminal session:
%WINDIR%\Microsoft.NET\Framework\v4.0.30319\ngen install EntityFramework.dll
While not related specifically to EF, here's a trick that should work with any query in general. The idea is to warmup your app via Application Initialization Module. So adding this in your web.config will cause this module to send a request to /startup
route to perform the initialization.
<applicationInitialization
doAppInitAfterRestart="true" >
<add initializationPage="/startup" />
</applicationInitialization>
With this view (/startup
), you can fire your heavy query (assuming its read only) for a test user which will cause EF to do all startup initialization.
See this answer for other tips to keep your app pool always running.
Upvotes: 3