为什么这个查询提升" SQL语句嵌套太深"?

时间:2016-03-29 12:06:02

标签: c# sql linq linq-to-entities

昨天,一位好人帮助我为PredicateBuilder here建立Linq to Entities
似乎工作正常,但完整的查询生成这个可怕的70 000行长here(太长时间无法粘贴),并提出SQL statement is nested too deeply

以下是上下文:
用户正在寻找符合其标准的动物列表,特别是关于能力的动物 在GUI中,对于每种能力类型(例如:"可操作性","敏捷性"等),用户可以选择修饰符(">", "<",或" =")和一个值 例如,他可能想要显示具有能力潜力的所有动物> 3敏捷"或"所有具有能力技能的动物< 10人的可操作性和能力潜力= 2敏捷性"

关于数据库:

PlayerId
Animal的{​​{1}}个 Id列:

  • Ability
  • Id
  • AnimalId(代表Enum,可以是"潜力"," BirthPotentiel"或"技能")
  • TypeId(代表Enum,可以是" Agility"或者" Maniability")
  • AbilityId

因此,每只动物的Value属性为AllAbilities

这是搜索功能(所有参数之前已由用户在GUI中输入或留空)。

ICollection<Ability>

能力过滤功能:

public async Task<List<Animal>> Search
    (
        Player player,
        int speciesId,
        int breedId,
        int coatId,
        int genderId,
        int minAge,
        int maxAge,
        int priceModifier, // int representing an Enum Criteria.ModifierE: ">", "<" or "="
        int priceValue,
        string ownerPseudo,
        bool isSearchingOwn,
        int minHeight,
        int maxHeight,
        int minWeight,
        int maxWeight,
        List<int> character, // representing list of Enum Flags
        List<int> abilitySkillModifiers, // representing list of Enum ModifierE: ">", "<" or "="
        List<int> abilitySkillValues,
        List<int> abilityPotentialModifiers, // representing list of Enum ModifierE: ">", "<" or "="
        List<int> abilityPotentialValues
    )
    {
        // You can see "PredicateUtils" class following the first link of this post
        var filter = PredicateUtils.Null<Animal>();

        filter = filter.And(e => speciesId != -1 ? e.SpeciesId == speciesId : true);
        filter = filter.And(e => breedId != -1 ? e.BreedId == breedId : true);
        filter = filter.And(e => coatId != -1 ? e.CoatId == coatId : true);
        filter = filter.And(e => genderId != -1 ? e.GenderId == genderId : true);
        filter = filter.And(e => minAge != -1 ? e.age >= minAge : true);
        filter = filter.And(e => maxAge != -1 ? e.age <= maxAge : true);

        string pseudo = isSearchingOwn ? player.Pseudo : ownerPseudo;
        filter = filter.And(e => !string.IsNullOrEmpty(ownerPseudo) ? e.Owner.Pseudo.Equals(pseudo, StringComparison.InvariantCultureIgnoreCase) : true);
        filter = filter.And(e => minHeight > 0 ? e.FinalHeight >= minHeight : true);
        filter = filter.And(e => maxHeight > 0 ? e.FinalHeight <= maxHeight : true);
        filter = filter.And(e => minWeight > 0 ? e.FinalWeight >= minWeight : true);
        filter = filter.And(e => maxWeight > 0 ? e.FinalWeight <= maxWeight : true);
        filter = filter.And(e => character.All(c => (e.character & c) == c));

        for (int i = 0; i < abilitySkillValues.Count; i++)
        {
            filter = filter.And(
                AbilitySkillFilter
                (
                    (Criteria.ModifierE)abilitySkillModifiers[i], // ">", "<", or "="
                    i,
                    abilitySkillValues[i] // value entered by the user for the current ability
                )
            );
        }

        for (int i = 0; i < abilityPotentialValues.Count; i++)
        {
            filter = filter.And(
                AbilityPotentialFilter
                (
                    (Criteria.ModifierE)abilityPotentialModifiers[i], // ">", "<", or "="
                    i,
                    abilityPotentialValues[i] // value entered by the user for the current ability
                )
            );
        }
        return await GetAll(filter);
    }

说明:
在数据库中,static Expression<Func<Animal, bool>> AbilitySkillFilter(Criteria.ModifierE modifier, int abilityId, int userValue) { if (modifier == Criteria.ModifierE.More) // User chose ">" return e => e.AllAbilities.Any(a => a.TypeId == (int)Ability.TypeE.Skill && a.AbilityId == abilityId) ? e.AllAbilities.FirstOrDefault(a => a.TypeId == (int)Ability.TypeE.Skill && a.AbilityId == abilityId).Value >= userValue : value <= 0; else if (modifier == Criteria.ModifierE.Equal) // User chose "<" return e => e.AllAbilities.Any(a => a.TypeId == (int)Ability.TypeE.Skill && a.AbilityId == abilityId) ? e.AllAbilities.FirstOrDefault(a => a.TypeId == (int)Ability.TypeE.Skill && a.AbilityId == abilityId).Value == userValue : value == 0; else if (modifier == Criteria.ModifierE.Less) // User chose "<" return e => e.AllAbilities.Any(a => a.TypeId == (int)Ability.TypeE.Skill && a.AbilityId == abilityId) ? e.AllAbilities.FirstOrDefault(a => a.TypeId == (int)Ability.TypeE.Skill && a.AbilityId == abilityId).Value <= userValue : value >= 0; else return null; } static Expression<Func<Animal, bool>> AbilityPotentialFilter(Criteria.ModifierE modifier, int abilityId, int userValue) { if (modifier == Criteria.ModifierE.More) return e => e.AllAbilities.Any(a => a.TypeId == (int)Ability.TypeE.Potential && a.AbilityId == abilityId) ? e.AllAbilities.FirstOrDefault(a => a.TypeId == (int)Ability.TypeE.Potential && a.AbilityId == abilityId).Value >= userValue : e.AllAbilities.FirstOrDefault(a => a.TypeId == (int)Ability.TypeE.BirthPotential && a.AbilityId == abilityId).Value >= userValue; else if (modifier == Criteria.ModifierE.Equal) return e => e.AllAbilities.Any(a => a.TypeId == (int)Ability.TypeE.Potential && a.AbilityId == abilityId) ? e.AllAbilities.FirstOrDefault(a => a.TypeId == (int)Ability.TypeE.Potential && a.AbilityId == abilityId).Value == userValue : e.AllAbilities.FirstOrDefault(a => a.TypeId == (int)Ability.TypeE.BirthPotential && a.AbilityId == abilityId).Value == userValue; else if (modifier == Criteria.ModifierE.Less) return e => e.AllAbilities.Any(a => a.TypeId == (int)Ability.TypeE.Potential && a.AbilityId == abilityId) ? e.AllAbilities.FirstOrDefault(a => a.TypeId == (int)Ability.TypeE.Potential && a.AbilityId == abilityId).Value <= userValue : e.AllAbilities.FirstOrDefault(a => a.TypeId == (int)Ability.TypeE.BirthPotential && a.AbilityId == abilityId).Value <= userValue; else return null; } Ability的{​​{1}}行可能不存在,而TypeId == Potential总是存在。

  • 如果当前动物和当前TypeId == Skill不存在TypeId == BirthPotential,我想将用户值与TypeId == Potential行值(始终存在)进行比较。
  • 如果当前动物和当前AbilityId不存在TypeId == BirthPotential,我想将用户值与0进行比较。

如果有人有任何关于为什么这个查询产生如此可怕的输出并且有所改进的建议,我将非常感激。 如果您需要更多信息,请不要犹豫。

解决方案:

最后,感谢TypeId == Skill提案(使用简单的AbilityId代替三元juharr而不添加条款,如果没有必要),并结合if解决方案。
根据年龄,性别,物种,伪,最小高度,最大高度,角色,一种技能和一种潜在能力的标准,这里是新的SQL输出:近70 000行到60! Result here

非常感谢!

1 个答案:

答案 0 :(得分:1)

进行动态过滤时,尝试在表达式之外进行更多静态评估。通过这种方式,您可以获得更好的查询,因为除了构建静态IN (...)列表条件之外,当前EF不会优化常量表达式。

但是您当前代码的主要问题是能力过滤器中FirstOrDefault的使用情况。通常尽量避免使用任何可能导致SQL子查询的查询构造类型,因为正如您所看到的,由于某种原因,EF嵌套了所有子查询,因此您可以获得那些怪异的SQL和错误。安全构造是使用Any,它被转换为没有嵌套的SQL EXISTS子查询。

所以试试这个,看看你会得到什么:

static Expression<Func<Animal, bool>> AbilitySkillFilter(Criteria.ModifierE modifier, int abilityId, int userValue)
{
    Expression<Func<GameAnimal, bool>> filter = null;
    bool includeMissing = false;
    if (modifier == Criteria.ModifierE.More) // User chose ">"
    {
        filter = e => e.AllAbilities.Any(a => a.TypeId == (int)Ability.TypeE.Skill && a.AbilityId == abilityId && a.Value >= userValue);
        includeMissing = userValue <= 0;
    }
    else if (modifier == Criteria.ModifierE.Equal) // User chose "="
    {
        filter = e => e.AllAbilities.Any(a => a.TypeId == (int)Ability.TypeE.Skill && a.AbilityId == abilityId && a.Value == userValue);
        includeMissing = userValue == 0;
    }
    else if (modifier == Criteria.ModifierE.Less) // User chose "<"
    {
        filter = e => e.AllAbilities.Any(a => a.TypeId == (int)Ability.TypeE.Skill && a.AbilityId == abilityId && a.Value >= userValue);
        includeMissing = userValue >= 0;
    }
    if (filter != null && includeMissing)
        filter = filter.Or(e => !e.AllAbilities.Any(a => a.TypeId == (int)Ability.TypeE.Skill && a.AbilityId == abilityId));
    return filter;
}

static Expression<Func<Animal, bool>> AbilityPotentialFilter(Criteria.ModifierE modifier, int abilityId, int userValue)
{
    if (modifier == Criteria.ModifierE.More)
        return e => e.AllAbilities.Any(a => a.TypeId == (int)Ability.TypeE.Potential && a.AbilityId == abilityId)
            ? e.AllAbilities.Any(a => a.TypeId == (int)Ability.TypeE.Potential && a.AbilityId == abilityId && a.Value >= userValue)
            : e.AllAbilities.Any(a => a.TypeId == (int)Ability.TypeE.BirthPotential && a.AbilityId == abilityId && a.Value >= userValue);
    else if (modifier == Criteria.ModifierE.Equal)
        return e => e.AllAbilities.Any(a => a.TypeId == (int)Ability.TypeE.Potential && a.AbilityId == abilityId)
            ? e.AllAbilities.Any(a => a.TypeId == (int)Ability.TypeE.Potential && a.AbilityId == abilityId && a.Value == userValue)
            : e.AllAbilities.Any(a => a.TypeId == (int)Ability.TypeE.BirthPotential && a.AbilityId == abilityId && a.Value == userValue);
    else if (modifier == Criteria.ModifierE.Less)
        return e => e.AllAbilities.Any(a => a.TypeId == (int)Ability.TypeE.Potential && a.AbilityId == abilityId)
            ? e.AllAbilities.Any(a => a.TypeId == (int)Ability.TypeE.Potential && a.AbilityId == abilityId && a.Value <= userValue)
            : e.AllAbilities.Any(a => a.TypeId == (int)Ability.TypeE.BirthPotential && a.AbilityId == abilityId && a.Value <= userValue);
    else
        return null;
}