2013-09-30 43 views
2

我正在處理一些EF查詢優化。爲了更好地理解查詢性能,我尋找到生成的SQL,並在一定條件讓我抓狂:生成的SQL查詢中的奇怪條件

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

我無法想象的情況下,當這個條件將評估爲false。也:

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(); 

UPDATE。 整個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] 

更新2。 這裏的實體類型,映射到AlgoVersions表:

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; } 
} 

其中VersionedEntity是基類的實體,其具有IdObjectVersion(二者EntityVersionedEntity沒有被映射到任何表):

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

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

你可以發佈整個生成的SQL語句嗎?很難單獨推斷這兩個條款的背景。 – jlew

+0

@jlew:當然可以。 – Dennis

+0

你可以發佈'AlgoVersions'類嗎? –

回答

1

我不確定爲什麼cast(1 as bit) <> cast(0 as bit)有,但有a work item in the EF backlog to remove this。我會推薦爲它投票。我想這意味着它完全無用,因爲該項目由RoMiller發佈。它在查詢中很常見,我經常見過它。