Extremely slow EF startup - 15 minutes

前端 未结 1 1530
一生所求
一生所求 2020-12-12 04:26

Some time ago I created a system, in which user can define categories with custom fileds for some objects. Then, each object has FieldValue based on its category. Classes be

相关标签:
1条回答
  • 2020-12-12 05:13

    I managed to reduce total start time cuased by EF 3 times with those tricks:

    1. Update framework to 6.2 and enable model caching:

      public class CachingContextConfiguration : DbConfiguration { public CachingContextConfiguration() { SetModelStore(new DefaultDbModelStore(Directory.GetCurrentDirectory())); }

      }

    2. Call ctx.Database.Initialize() explicitly from new thread, as early as possible. This still takes 3-4 seconds, but since it happens alongside other things, it helps a lot.

    3. Load entities into EF cache in reasonable order.

    Previously, I just wrote Include after Inlude, which translates into multiple joins. I found a "rule of thumb" on some blog posts, that up to two chained Includes EF performs rather well, but each more slows everything down massively. I also found a blog post, that showed EF caching: once given entity was loaded with Include or Load, it will be automatically put in proper property (blog author is wrong about union of objects). So I did this:

      using (var db = new MyContext())
                {
                    db.Fields.Load();
                    db.Categories.Include(c => c.MainField).Include(x => x.Fields).Load();
                    db.FieldValues.Load();
                    return db.Objects.Include(x => x.MainFieldValue.Field).ToArray();
                } 
    

    This is fetching data 6 times faster than includes from question. I think that once entities are previously loaded, EF engine does not call database for related objects, it just gets them from cache.

    1. I also added this in my context constructor:

          Configuration.LazyLoadingEnabled = false;
          Configuration.ProxyCreationEnabled = false;
      

    Effects of that are barely noticable, but may play bigger role on huge data set.

    I've also watched this presentation of EF Core by Rowan Miller and I will be switching to it on next release - in some cases it's 5-6 times faster than EF6.

    Hope this helps someone

    0 讨论(0)
提交回复
热议问题