2

我正在做一些 EF 查询优化。为了更好地了解查询性能,我正在研究生成的 SQL,有些情况让我抓狂:

WHERE (cast(1 as bit) <> cast(0 as bit))...

我无法想象这种情况会评估为假的情况。还:

CASE WHEN ([Extent9].[Id] IS NULL) THEN...

显然,[Extent9].[Id] 表示对应表中的主键列,当然不可能是NULL.

为什么这些条件出现在 SQL 中?这是 LINQ 查询文本,如果它可以帮助回答问题:

var query = _context
    .Objects
    .Where(o => o.DatawareVersionId == _datawareId);
    .SelectMany(t => t.ComplexSubTasks)
    .Where(st => st.Variant.Order.CurrentAlgoVersion != null)
    .Select(st => st.Variant.Order.CurrentAlgoVersion)
    .OfType<MonitoringAlgoVersion>()
    .Where(version => version != null && version.Applicabilities.Any(applicability => applicability.SymbolicCircuitTypeId == _typeId))
    .Any());
    .Select(o => o.Group)
    .Distinct();

更新。整个 SQL 语句:

SELECT 
[Distinct1].[Id] AS [Id], 
[Distinct1].[GenericGroup] AS [GenericGroup], 
[Distinct1].[Number] AS [Number], 
[Distinct1].[Group] AS [Group]
FROM ( SELECT DISTINCT 
    [Extent2].[Id] AS [Id], 
    [Extent2].[GenericGroup] AS [GenericGroup], 
    [Extent2].[Number] AS [Number], 
    [Extent2].[Group] AS [Group]
    FROM  [dbo].[Objects] AS [Extent1]
    INNER JOIN [dbo].[ObjectGroups] AS [Extent2] ON [Extent1].[GroupId] = [Extent2].[Id]
    WHERE ([Extent1].[DatawareVersionId] = @p__linq__0) AND ( EXISTS (SELECT 
        1 AS [C1]
        FROM ( SELECT 
            [Extent9].[Id] AS [Id]
            FROM       [dbo].[Tasks] AS [Extent3]
            INNER JOIN [dbo].[SubTasks] AS [Extent4] ON [Extent3].[Id] = [Extent4].[TaskId]
            LEFT OUTER JOIN [dbo].[Variants] AS [Extent5] ON [Extent4].[VariantId] = [Extent5].[Id]
            LEFT OUTER JOIN [dbo].[Orders] AS [Extent6] ON [Extent5].[OrderId] = [Extent6].[Id]
            INNER JOIN [dbo].[AlgoVersions] AS [Extent7] ON [Extent6].[CurrentAlgoVersion] = [Extent7].[Id]
            LEFT OUTER JOIN [dbo].[Orders] AS [Extent8] ON [Extent5].[OrderId] = [Extent8].[Id]
            LEFT OUTER JOIN [dbo].[AlgoVersions] AS [Extent9] ON [Extent8].[CurrentAlgoVersion] = [Extent9].[Id]
            WHERE (cast(1 as bit) <> cast(0 as bit)) AND ([Extent1].[Id] = [Extent3].[ObjectId]) AND (CASE WHEN ([Extent9].[Id] IS NULL) THEN CAST(NULL AS varchar(1)) ELSE '10X0X' END LIKE '10X0X%')
        )  AS [Project1]
        WHERE  EXISTS (SELECT 
            1 AS [C1]
            FROM [apot].[draw_algo_versions_applicability] AS [Extent10]
            WHERE ([Project1].[Id] = [Extent10].[algo_version_id]) AND ([Extent10].[schema_type_id] = @p__linq__1)
        )
    ))
)  AS [Distinct1]

更新 2AlgoVersions这是映射到表的实体类型:

public abstract class AlgoVersion : VersionedEntity
{           
    public virtual int OrderId { get; set; }
    public virtual Order Order { get; set; }
    public virtual bool IsErrorFixed { get; set; }
    public virtual bool IsFunctionalityExtended { get; set; }
    public virtual ObservableCollection<AlgoVersionStatus> Statuses { get; set; }
}

public class MonitoringAlgoVersion : AlgoVersion
{
    public virtual ObservableCollection<TemplateGroup> TemplateGroups { get; set; }
    public virtual ObservableCollection<MonitoringAlgoVersionApplicability> Applicabilities { get; set; }
}

whereVersionedEntity是实体的基类,它有一个IdObjectVersion(两者EntityVersionedEntity没有映射到任何表):

public abstract class Entity
{
    public virtual int Id { get; set; }
}

public abstract class VersionedEntity : Entity
{
    public virtual byte[] ObjectVersion { get; set; }
}
4

1 回答 1

1

我不确定为什么 cast(1 as bit) <> cast(0 as bit)会存在,但EF 积压中有一个工作项可以删除它。我建议为它投票。我想这意味着它完全没用,因为该项目是由 RoMiller 发布的。它在查询中很常见,我以前经常看到它。

于 2013-09-30T14:51:44.870 回答