如何优化下面的SQL查询

时间:2019-11-07 12:07:45

标签: sql-server

如何优化以下查询。

ALTER FUNCTION  [dbo].[GetVostroAccountByAccountWithInstitution]  

(    
 @P_AccountWithInstitution varchar(150)
)  
RETURNS VARCHAR(15)  
AS   
    BEGIN               
            DECLARE  @VostroAccount VARCHAR(15)  

            IF EXISTS(SELECT 1 FROM VostroAccConfig WHERE SwiftCode=@P_AccountWithInstitution)
                BEGIN
                    SET @VostroAccount=(SELECT VostroAcc FROM VostroAccConfig WHERE SwiftCode=@P_AccountWithInstitution)
                END
            ELSE IF EXISTS(SELECT 1 FROM VostroAccConfig WHERE SwiftCode8=@P_AccountWithInstitution)
                BEGIN
                    SET @VostroAccount=(SELECT VostroAcc FROM VostroAccConfig WHERE SwiftCode8=@P_AccountWithInstitution)
                END

            RETURN @VostroAccount
    END

1 个答案:

答案 0 :(得分:0)

第一步是不使用标量函数。内联表值函数将执行得更好。为了使表函数内联,它必须是单个查询。幸运的是,您可以使用发布的功能来做到这一点。

create function [dbo].[GetVostroAccountByAccountWithInstitution]  
(    
    @P_AccountWithInstitution varchar(150)
) returns table as return
    select top 1 VostroAcc
    from VostroAccConfig
    where @P_AccountWithInstitution in (SwiftCode, SwiftCode8)
    order by SwiftCode
        , SwiftCode8

快速执行演奏的下一步是索引。但由于不了解表格,因此很难提出建议。

相关问题