How can I view the Query Entity Framework LINQ cache plan?

I'm having issues with slow compilation of LINQ queries in EF6. I know that EF caches compiled query plans for LINQ queries, but there are some gotchas (for example, Enumerable.Contains prevents caching). I would like to review the cache for debugging purposes to check if I really get the correct caching for my requests. How can i do this?

Note: since this is purely for debugging, I would be pleased with the answer using reflection or other means that will not be used in production.

+4
source share
2 answers

, - ( EF 6.1.3):

var method = context.Database.GetType().GetMethod("CreateStoreItemCollection", BindingFlags.Instance | BindingFlags.NonPublic);
var storeItemsCollection = method.Invoke(context.Database, null);
var queryCacheManagerField = storeItemsCollection.GetType().GetField("_queryCacheManager", BindingFlags.Instance | BindingFlags.NonPublic);
var queryCacheManager = queryCacheManagerField.GetValue(storeItemsCollection);
var cacheField = queryCacheManager.GetType().GetField("_cacheData", BindingFlags.Instance | BindingFlags.NonPublic);
var cacheData = cacheField.GetValue(queryCacheManager) as ICollection;
foreach (var item in cacheData)
{
    Console.WriteLine(item.ToString());
}

, internal ( System.Data.Entity.Core.Common.QueryCache), . , CompiledQueryCacheKey ToString, () . (Table.Count()) :

[System.Data.Entity.Core.Common.QueryCache.ShaperFactoryQueryCacheKey`1[System.Int32], System.Data.Entity.Core.Common.QueryCache.QueryCacheEntry]

[FUNC<Edm.Count(In Transient.collection[Edm.Int32(Nullable=True,DefaultValue=)](Nullable=True,DefaultValue=))>:ARGS(([Project](BV'LQ1'=([Scan](DashboardAutoContext.Organizations:Transient.collection[DashboardAuto.Organization(Nullable=True,DefaultValue=)]))(1:Edm.Int32(Nullable=True,DefaultValue=)))))|||AppendOnly|True, System.Data.Entity.Core.Common.QueryCache.QueryCacheEntry]

, , , , .

, , - , DbContext.Database.Log . , " ...", , " ...". , EF LINQ SQL. , ; , .

(, .)

+3

IDbCommandTreeInterceptor . , "" .

+2

Source: https://habr.com/ru/post/1623814/


All Articles