Very strange problem happening with the Entity Framework for our ASP.NET web application hosted on IIS. We have a simple query that continues to run slower the longer the application is running. The issue so far cannot be replicated when debugging locally through visual studio.
var query = context.Stocktakes
.Include(s => s.Items)
.Include(s => s.Items.Select(i => i.Product))
.Where(s => s.StocktakeID == stocktakeID && s.SiteID == siteID)
.AsNoTracking()
.SingleOrDefault();
After restarting the app pool, the query runs in less than ¼ of a second. Once there has been a small amount of traffic on the application the query performance starts to degrade, taking 6 seconds and so far up to 22 seconds after several hours of the application being hosted.
The same query when ran through SQL Server Manager takes less than ¼ of a second always. Clearing the execution plans has little to no effect so I do not believe the issue is due to parameter sniffing. I have created a copy of the application running under the same app pool, and under a different app pool with only myself connecting to it and the query performance does not degrade, it remains fast with the query taking less than ¼ of a second.
Does anyone have any ideas on what is happening?
Edit: The problem does not relate to nested projections as there is no projection to a non-entity class in the example given. The example is showing querying entities direct from the context. The link given for nested projections also does not explain why the query performance would degrade over time.