除了已编译的查询,如何改善冷查询

时间:2019-01-15 19:09:37

标签: entity-framework-core ef-core-2.1

我的存储库上有一个Find API,并且编译后的查询无法正常工作。该查询具有多个IEnumerable.Contains(),因此在SQL中的编译结果没有where-in语句,并且所有包含的内容都在本地求值。

我知道Entity Framework 6支持视图预生成,Entity Framework核心是否具有类似的概念?如果没有,那么提高冷查询性能的下一个技巧是什么?

[更新] 这仅用于说明

var typeIds = new int[] { 1, 2 };
var allTypeIds = !typeIds.Any();
var subTypeIds = new int[0];
var allSubTypeIds = !subTypeIds.Any();
var navEntityIds = new int[0];
var allNavEntityIds = !navEntityIds.Any();

var result = dbContext.Entities.Where(e =>
    (allTypeIds || typeIds.Contains(e.TypeId))
    && (allSubTypeIds || subTypeIds.Contains(e.SubTypeId))
    && (allNavEntityIds || navEntityIds.Contains(e.NavigationEntityId)))
    .Include(e => e.Type)
    .Include(e => e.SubType)
    .Include(e => e.NavigationEntity)
    .Include(e => e.ExtendedEntities)
    .ToList();

[Update2]编译版本

var searchParam = new SearchParam();
searchParam.TypeIds = new int[] { 1, 2 };
searchParam.AllTypeIds = !searchParam.TypeIds.Any();
searchParam.SubTypeIds = new int[0];
searchParam.AllSubTypeIds = !searchParam.SubTypeIds.Any();
searchParam.NavEntityIds = new int[0];
searchParam.AllNavEntityIds = !searchParam.NavEntityIds.Any();

var compiledQuery = EF.CompileQuery<MyDbContext, SearchParam, Entity>((ctx, sp) =>
    ctx.Entities.Where(e =>
        (sp.AllTypeIds || sp.TypeIds.Contains(e.TypeId))
        && (sp.AllSubTypeIds || sp.SubTypeIds.Contains(e.SubTypeId))
        && (sp.AllNavEntityIds || sp.NavEntityIds.Contains(e.NavigationEntityId)))
        .Include(e => e.Type)
        .Include(e => e.SubType)
        .Include(e => e.NavigationEntity)
        .Include(e => e.ExtendedEntities));
var result = compiledQuery.Invoke(dbContext, searchParam)
    .ToList();

0 个答案:

没有答案