查询性能是否受布尔文字影响?

时间:2019-07-16 16:50:27

标签: c# .net performance entity-framework linq

我想知道布尔文字是否会影响Entity Framework中的查询性能,因为有人告诉我它们确实会影响查询框架的性能,尽管我找不到任何与之相符的材料,并且我认为使用它们是多余的... >

下面是我所做的一个示例:

_context.Entity.Where(e => e.Blocked);

我被告知以下方法由于性能会更好:

_context.Entity.Where(e => e.Blocked == true);

很抱歉遇到任何英语错误,这不是我的母语。 :/

1 个答案:

答案 0 :(得分:1)

实体框架有关性能的主要问题与开发人员开发查询的方式有关。如果查询写得正确,则无需担心。

确保有关所生成查询的最简单方法是使用SQL事件探查器。您可以在那里简单地跟踪查询。为了进一步学习,您可以阅读this

在您的情况下,两个生成的查询都是相同的。

我在下面开发了一个示例代码。

假设这些查询

var q1 = _roleRepository.GetAll().Where(q => q.IsDeleted).ToList();

var q2 = _roleRepository.GetAll().Where(q => q.IsDeleted == true).ToList();

SQL事件探查器中的结果跟踪是:

exec sp_executesql N'SELECT 
        [Extent1].[Id] AS [Id], 
        [Extent1].[NormalizedName] AS [NormalizedName], 
        [Extent1].[TenantId] AS [TenantId], 
        [Extent1].[Name] AS [Name], 
        [Extent1].[DisplayName] AS [DisplayName], 
        [Extent1].[IsStatic] AS [IsStatic], 
        [Extent1].[IsDefault] AS [IsDefault], 
        [Extent1].[IsDeleted] AS [IsDeleted], 
        [Extent1].[DeleterUserId] AS [DeleterUserId], 
        [Extent1].[DeletionTime] AS [DeletionTime], 
        [Extent1].[LastModificationTime] AS [LastModificationTime], 
        [Extent1].[LastModifierUserId] AS [LastModifierUserId], 
        [Extent1].[CreationTime] AS [CreationTime], 
        [Extent1].[CreatorUserId] AS [CreatorUserId]
        FROM [dbo].[AbpRoles] AS [Extent1]
        WHERE ((([Extent1].[TenantId] IS NULL) AND (@DynamicFilterParam_000003 IS NULL)) OR (([Extent1].[TenantId] IS NOT NULL) AND (([Extent1].[TenantId] = @DynamicFilterParam_000003) OR (([Extent1].[TenantId] IS NULL) AND (@DynamicFilterParam_000003 IS NULL)))) ) AND (([Extent1].[IsDeleted] = @DynamicFilterParam_000001) ) AND ([Extent1].[IsDeleted] = 1)',N'@DynamicFilterParam_000003 int,@DynamicFilterParam_000004 bit,@DynamicFilterParam_000001 bit,@DynamicFilterParam_000002 bit',@DynamicFilterParam_000003=1,@DynamicFilterParam_000004=NULL,@DynamicFilterParam_000001=0,@DynamicFilterParam_000002=NULL

        exec sp_executesql N'SELECT 
        [Extent1].[Id] AS [Id], 
        [Extent1].[NormalizedName] AS [NormalizedName], 
        [Extent1].[TenantId] AS [TenantId], 
        [Extent1].[Name] AS [Name], 
        [Extent1].[DisplayName] AS [DisplayName], 
        [Extent1].[IsStatic] AS [IsStatic], 
        [Extent1].[IsDefault] AS [IsDefault], 
        [Extent1].[IsDeleted] AS [IsDeleted], 
        [Extent1].[DeleterUserId] AS [DeleterUserId], 
        [Extent1].[DeletionTime] AS [DeletionTime], 
        [Extent1].[LastModificationTime] AS [LastModificationTime], 
        [Extent1].[LastModifierUserId] AS [LastModifierUserId], 
        [Extent1].[CreationTime] AS [CreationTime], 
        [Extent1].[CreatorUserId] AS [CreatorUserId]
        FROM [dbo].[AbpRoles] AS [Extent1]
        WHERE ((([Extent1].[TenantId] IS NULL) AND (@DynamicFilterParam_000003 IS NULL)) OR (([Extent1].[TenantId] IS NOT NULL) AND (([Extent1].[TenantId] = @DynamicFilterParam_000003) OR (([Extent1].[TenantId] IS NULL) AND (@DynamicFilterParam_000003 IS NULL)))) ) AND (([Extent1].[IsDeleted] = @DynamicFilterParam_000001) ) AND (1 = [Extent1].[IsDeleted])',N'@DynamicFilterParam_000003 int,@DynamicFilterParam_000004 bit,@DynamicFilterParam_000001 bit,@DynamicFilterParam_000002 bit',@DynamicFilterParam_000003=1,@DynamicFilterParam_000004=NULL,@DynamicFilterParam_000001=0,@DynamicFilterParam_000002=NULL

完全一样!