如何优化查询,在sql查询中替换IN?

时间:2015-02-12 05:43:22

标签: sql sql-server sql-server-2008

我的查询需要花费更多时间。我发现是因为查询中的IN运算符。我需要优化它,请提示

查询

DECLARE @LevelList TABLE(BadgeNo NVARCHAR(200))  
INSERT INTO @LevelList(BadgeNo) EXEC spDataLevelSecurity SIP008548 
declare @RetCountValueOUT nvarchar(3)
SELECT  @RetCountValueOUT = COUNT([Badge No] ) FROM (       
        SELECT
            Distinct
            BC.BadgeNo AS [Badge No],
            dbo.ProperCase(FirstName + + ' ' + ISNULL(MiddleName,'') + CASE WHEN ISNULL(MiddleName,'') <> '' THEN ' ' ELSE '' END + ISNULL(LastName,'')) AS [Employee Name],
            ISNULL(Job.BusinessUnit,'') AS [Company],
            ' By ''' + dbo.ProperCase(F1.UserFullName) + ''' On ''' + REPLACE(CONVERT(NVARCHAR(11),CreatedOn,106),' ','-') + '''' AS [Creation Details],
            ' By ''' + dbo.ProperCase(F2.UserFullName) + ''' On ''' + REPLACE(CONVERT(NVARCHAR(11),VerifiedOn,106),' ','-') + '''' AS [Verification Details]
        FROM    
            Componentdetails BC 
        INNER JOIN Job 
            ON Job.BadgeNo = BC.BadgeNo
        INNER JOIN Employee
            ON Employee.BadgeNo = BC.BadgeNo
        LEFT JOIN FalconUsers F1
            ON F1.EmployeeID = BC.CreatedBy 
        LEFT JOIN FalconUsers F2
            ON F2.EmployeeID = BC.VerifiedBy 
        WHERE
            Isverified  = 1
            AND IsApproved = 0
            AND RejectTag = 0
        )A 
where a.[Badge No] in   ( Select BadgeNo from @LevelList )
select @RetCountValueOUT

提前致谢

1 个答案:

答案 0 :(得分:1)

使用INNER JOIN

DECLARE @LevelList TABLE(BadgeNo NVARCHAR(200))  
INSERT INTO @LevelList(BadgeNo) EXEC spDataLevelSecurity SIP008548 
declare @RetCountValueOUT nvarchar(3)
SELECT  @RetCountValueOUT = COUNT([Badge No] ) FROM (       
        SELECT
            Distinct
            BC.BadgeNo AS [Badge No],
            dbo.ProperCase(FirstName + + ' ' + ISNULL(MiddleName,'') + CASE WHEN ISNULL(MiddleName,'') <> '' THEN ' ' ELSE '' END + ISNULL(LastName,'')) AS [Employee Name],
            ISNULL(Job.BusinessUnit,'') AS [Company],
            ' By ''' + dbo.ProperCase(F1.UserFullName) + ''' On ''' + REPLACE(CONVERT(NVARCHAR(11),CreatedOn,106),' ','-') + '''' AS [Creation Details],
            ' By ''' + dbo.ProperCase(F2.UserFullName) + ''' On ''' + REPLACE(CONVERT(NVARCHAR(11),VerifiedOn,106),' ','-') + '''' AS [Verification Details]
        FROM    
            Componentdetails BC 
        INNER JOIN
            Job 
        ON
            Job.BadgeNo = BC.BadgeNo
        INNER JOIN
            Employee
        ON
            Employee.BadgeNo = BC.BadgeNo
        LEFT JOIN
            FalconUsers F1
        ON
            F1.EmployeeID = BC.CreatedBy 
        LEFT JOIN
            FalconUsers F2
        ON
            F2.EmployeeID = BC.VerifiedBy 
        WHERE
            Isverified  = 1
        AND
            IsApproved = 0
        AND
            RejectTag = 0
        )A 
        INNER JOIN @LevelList AS B 
        ON a.[Badge No] = B.BadgeNo
select @RetCountValueOUT