Search code examples
sql-serverentity-frameworkwarm-up

Entity Framework 6.2 database first warm up


I have an EF 6.2 project in my MVC solution.

This uses a SQL server db and has about 40 tables with many foreign keys.

The first query is very slow, 20 secs.

I immediately hit the same page again, changing the user param, and the query takes less than 1 second.

So this looks like a warm up issue in EF6. That's fine and there's loads of things i can do to sort apparently.

  1. The Model cache (part of EF6.2) looks like it could be beneficial, but everywhere i read about it states model first. Nothing about DB first. Would this still work with db first?
  2. Also there's the Entity Framework 6 power tools, these allow for me to Generate Views. Tried this and it doesn't seem to make any difference. Is this still a valid route?
  3. Any other ideas?

Solution

  • So, the answer was to update EF to 6.2 then use the newest feature:

    public class MyDbConfiguration : DbConfiguration
    {
        public MyDbConfiguration() : base()
        {
            var path = Path.GetDirectoryName(this.GetType().Assembly.Location);
            SetModelStore(new DefaultDbModelStore(path));
        }
    }
    

    for the full story check out this link: https://entityframework.net/why-first-query-slow

    Your gonna take a small performance hit at startup but then it all moves a lot faster.

    For anyone using an Azure web app you can use a deployment slot (https://stackify.com/azure-deployment-slots/), this allow you to publish into a non-production slot then warm it up before swapping it in as the production slot.