存储过程

时间:2017-01-16 06:20:27

标签: sql-server optimization

我需要提高SQL Server 2014中存储过程的性能。

这是我的程序,我希望优化此程序,因为这会慢慢运行。

有人可以用一些例子来解释,哪种查询在什么情况下以及在什么情况下更好?

CREATE PROCEDURE [dbo].[sp_Customer_SendMoney] 
    @CustomerId bigint,    
    @MobileNo nvarchar(15),    
    @Amount money,    
    @Comment nvarchar(250),    
    @PassPhrase nvarchar(50),
    @Type varchar(50) = null    
AS
BEGIN
    DECLARE @returnCode INT 
    DECLARE @RetVal BIGINT, @CustomerBalance MONEY,
            @CustomerMoneyRequestedId BIGINT, 
            @ToCustomerId BIGINT = 0,
            @TransactionId BIGINT, @CustomerAccount BIGINT,    
            @FromCustomerBalance MONEY = 0,
            @ToCustomerBalance MONEY = 0   

    IF EXISTS (SELECT Id FROM Customer 
               WHERE Id = @CustomerId AND IsDeleted = 0 AND IsActive = 1)    
    BEGIN
        SELECT 
            @CustomerBalance = Balance 
        FROM
            Customer 
        WHERE 
            Id = @CustomerId AND IsDeleted = 0 AND IsActive = 1 
       select @ToCustomerId = Id, @CustomerAccount = AccountNo,@ToCustomerBalance=Balance From Customer where convert(nvarchar,DecryptByPassPhrase(@PassPhrase, MobileNo)) = @MobileNo  and IsDeleted = 0 and IsActive = 1    
            if(@ToCustomerId > 0)
        begin
         if( lower(isnull(@Type,'regular')) <> 'suspention')
         begin
          set @ToCustomerBalance=@ToCustomerBalance+@Amount
         end
        END

        set @FromCustomerBalance=@CustomerBalance-@Amount
        if((@CustomerBalance > 0) and (@CustomerBalance >= @Amount) )    
        Begin     
           BEGIN TRAN TxnsenMoney
           BEGIN TRY 
            select @TransactionId = TransactionW2W+1 from MstGenerateTransactionID 
            where  [year]=datepart(yyyy,getdate()) and [month]=DATENAME(month,getdate())

            update MstGenerateTransactionID set TransactionW2W= @TransactionId 
            where  [year]=datepart(yyyy,getdate()) and [month]=DATENAME(month,getdate())
           --set @TransactionId = CONVERT(bigint,replace(convert(varchar, getdate(),111),'/','') + replace(convert(varchar, getdate(),114),':',''))       
        IF(@ToCustomerId > 0)    
        BEGIN
       --Update sender Customer
       update Customer set Balance = Balance - @Amount  where Id = @CustomerId    
       --Update receiver Customer
       if(lower(isnull(@Type,'regular')) <> 'suspention')
        begin
         update Customer set Balance = Balance + @Amount  where Id = @ToCustomerId   
        end  
       else
        begin
         update Customer set SuspentionAccount = isnull(SuspentionAccount,0) + @Amount  where Id = @ToCustomerId   
        end  

       INSERT INTO [TransactionW2W]    
        ([TransactionId]    
        ,[FromCustomerId]    
        ,[ToCustomerId]    
        ,[MobileNo]    
        ,[Amount]    
        ,[Comments]    
        ,[CreatedOn]
        ,[FromCustomerBalance]
        ,[ToCustomerBalance])    
        VALUES    
        (@TransactionId    
        ,@CustomerId    
        ,@ToCustomerId    
        ,@MobileNo    
        ,@Amount    
        ,@Comment    
        ,GETDATE()
        ,@FromCustomerBalance
        ,@ToCustomerBalance)                     

        End    --end IF @ToCustomerId > 0
        ELSE    
        BEGIN 
       --Update sender Customer              
       update Customer set Balance = Balance - @Amount where Id = @CustomerId    
       --print 'ELSE'
       INSERT INTO [TransactionW2W]    
       ([TransactionId]    
       ,[FromCustomerId]    
       ,[ToCustomerId]    
       ,[MobileNo]    
       ,[Amount]    
       ,[Comments]    
       ,[CreatedOn]
       ,[FromCustomerBalance])    
       VALUES    
       (@TransactionId    
       ,@CustomerId    
       ,@ToCustomerId    
       ,@MobileNo    
       ,@Amount    
       ,@Comment    
       ,GETDATE()
       ,@FromCustomerBalance)     

       INSERT INTO [NewCustomer]    
         ([FromCustomerId]    
         ,[MobileNo]    
         ,[Amount]    
         ,[CreatedOn]   
           )    
         VALUES    
         (@CustomerId    
         ,@MobileNo    
         ,@Amount    
         ,GETDATE()
          )    
        END --end ELSE @ToCustomerId > 0

     print @RetVal


      IF(@@TRANCOUNT >0 )    
         begin    
      set @RetVal = @TransactionId 
      print @RetVal      
         end    
         else    
         begin    
      RAISERROR('records not executed',16,1)    
         END
      COMMIT TRAN TxnsenMoney    
      END TRY  
      BEGIN CATCH 
      ROLLBACK TRAN TxnsenMoney
      set @RetVal = -1
      declare @error varchar(max)
      set @error= ERROR_MESSAGE()
     -- RAISERROR(@error,16,1) 
     print @error
      END CATCH     
    select @RetVal   
    End 
    END
    End

1 个答案:

答案 0 :(得分:-1)

CREATE PROCEDURE [dbo].[sp_Customer_SendMoney] 
    @CustomerId bigint,    
    @MobileNo nvarchar(15),    
    @Amount money,    
    @Comment nvarchar(250),    
    @PassPhrase nvarchar(50),
    @Type varchar(50) = null    
    AS    
    Begin    
       DECLARE @returnCode INT ,@RetVal bigint,@CustomerBalance money, @CustomerMoneyRequestedId bigint,@ToCustomerId bigint = 0,@TransactionId bigint,@CustomerAccount bigint,    
               @FromCustomerBalance money=0,@ToCustomerBalance money=0   

                IF EXISTS (select Id from Customer WITH(NOLOCK) where Id = @CustomerId and IsDeleted = 0 and IsActive = 1)  Begin    

                select @CustomerBalance = Balance from Customer WITH(NOLOCK) where Id = @CustomerId and IsDeleted = 0 and IsActive = 1 
                select @ToCustomerId = Id, @CustomerAccount = AccountNo,@ToCustomerBalance=Balance From Customer WITH(NOLOCK) 
                where convert(nvarchar,DecryptByPassPhrase(@PassPhrase, MobileNo)) = @MobileNo  and IsDeleted = 0 and IsActive = 1    
                    IF(@ToCustomerId > 0)
                        BEGIN
                        if( lower(isnull(@Type,'regular')) <> 'suspention')
                        BEGIN
                        SET @ToCustomerBalance=@ToCustomerBalance+@Amount
                    END
                END

        SET @FromCustomerBalance=@CustomerBalance-@Amount

       if((@CustomerBalance > 0) and (@CustomerBalance >= @Amount) )    
        Begin     
           BEGIN TRAN TxnsenMoney
                   BEGIN TRY 
                    SELECT  @TransactionId = TransactionW2W+1 
                    FROM    MstGenerateTransactionID  WITH(NOLOCK)
                    WHERE   [year]=datepart(yyyy,getdate()) and [month]=DATENAME(month,getdate())

                    update MstGenerateTransactionID set TransactionW2W= @TransactionId 
                    where  [year]=YEAR(GETDATE()) and [month]= MONTH(GETDATE())

               --set @TransactionId = CONVERT(bigint,replace(convert(varchar, getdate(),111),'/','') + replace(convert(varchar, getdate(),114),':',''))       
        IF(@ToCustomerId > 0)  BEGIN
       --Update sender Customer
             UPDATE Customer set Balance = Balance - @Amount  where Id = @CustomerId    
       --Update receiver Customer

        if(lower(isnull(@Type,'regular')) <> 'suspention') BEGIN 

             UPDATE Customer set Balance = Balance + @Amount  where Id = @ToCustomerId   

       END ELSE BEGIN 

           UPDATE Customer set SuspentionAccount = isnull(SuspentionAccount,0) + @Amount  where Id = @ToCustomerId   
       END  

       INSERT INTO [TransactionW2W]    
                  ([TransactionId] ,[FromCustomerId],[ToCustomerId] ,[MobileNo] ,[Amount],[Comments],[CreatedOn],[FromCustomerBalance],[ToCustomerBalance])    

       SELECT      @TransactionId ,@CustomerId ,@ToCustomerId ,@MobileNo ,@Amount ,@Comment,GETDATE(),@FromCustomerBalance ,@ToCustomerBalance                   

        END    --end IF @ToCustomerId > 0
        ELSE BEGIN 
           --Update sender Customer              
             UPDATE Customer set Balance = Balance - @Amount where Id = @CustomerId    
           --print 'ELSE'
             INSERT INTO [TransactionW2W]    
                       ([TransactionId] ,[FromCustomerId],[ToCustomerId] ,[MobileNo] ,[Amount] ,[Comments] ,[CreatedOn],[FromCustomerBalance])    

            SELECT @TransactionId ,@CustomerId  ,@ToCustomerId ,@MobileNo ,@Amount ,@Comment ,GETDATE() ,@FromCustomerBalance

            INSERT INTO [NewCustomer]    
                   ([FromCustomerId] ,[MobileNo] ,[Amount] ,[CreatedOn] )    

            SELECT @CustomerId,@MobileNo ,@Amount,GETDATE()

        END --end ELSE @ToCustomerId > 0

     print @RetVal


      IF(@@TRANCOUNT >0 )BEGIN
          set @RetVal = @TransactionId 
          print @RetVal      
     END ELSE BEGIN 
                  RAISERROR('records not executed',16,1)    
      END

      COMMIT TRAN TxnsenMoney    
      END TRY  

      BEGIN CATCH 

     ROLLBACK TRAN TxnsenMoney
      SET @RetVal = -1
      DECLARE @error varchar(max)
      SET @error= ERROR_MESSAGE()
     -- RAISERROR(@error,16,1) 
     print @error
      END CATCH     
    select @RetVal   
    End 
    END
    End
相关问题