简体   繁体   中英

Entity Framework 6 generating model performance

I have an issue with EF6 on a web development. In the index page I need to execute a query to display some info, the problem is that the first query creates the model on memory and takes over 10 secs to do it (is not in a great hosting). How people deal with this? Get first data with a direct query? read a txt file?

By default in EF it has a snapshot change tracking mechanism.

It works by saving entity states each time when it is loaded from the database. When for example SaveChanges method is called, Entity Framework checks all entities in current context and compares them with saved state.

This can be a touch slow sometimes... in the context ctor chuck this in:

this.Configuration.AutoDetectChangesEnabled = false;

this turns off entity tracking which should take away a bit of the overhead here.

However, if it is fairly static data, perhaps you can use a cache to hold on to the data when the app starts up then you only need to pull it from memory and have it refresh every x hours/minutes.

Also, as an aside, move to AZURE if you can, it won't cost much more I expect and will be a lot easier to work with for deployments etc.

There are two things I'd do: 1) Profile your query and see if it really needs to take 10 seconds. Check out LINQPad, Glimpse, or Stackify prefix for ways to view the SQL query that is generated from EF. An inefficient query might be your problem. Using a view in your DB or setting indexes may fix the problem.

2) If you want to run this query once and then use an in-memory copy for subsequent requests, use MemoryCache . There will still be a performance hit for the first request, but all subsequent requests will be incredibly fast for as long as the cache is set to last. There is a memory hit when using this because you're keeping the results in memory long-term. If every visitor has a dynamic request on the index page, you will use a lot of memory. If the request is the same for all users, this will be a great option.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM