Quantcast
Channel: Forum Getting started with SQL Server
Viewing all articles
Browse latest Browse all 7129

How to optimise SQL Stored Procedure to remove deadlocks

$
0
0

Hello All,

I am asked to optimize a SQL stored Proc but I don't have very limited knowledge of SQL so need help with the same. we have a stored proc which when called many times at a time from different machines starts giving deadlock errors. I am thinking of adding a with (nolock) statement wherever a select query is written in the stored proc. Apart from that what else can be done in the SP below:

USE [STP_Biz]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER proc [dbo].[sp_Result]

      @btMsgId uniqueidentifier ,

      @Result char(3) ,

      @Data varchar(64),

      @Advisory varchar(250),

      @Stamp varchar(50),

      @Function char(6) = null

as      

INSERT Result

(

      btMsgId,

      Result,

      Data,

      Advisory,

      Stamp,

      Function
)

values

(

      @btMsgId,

      @Result,

      rtrim(@Data),

      @Advisory,

      @Stamp,

      @Function

)

 

IF (@Result = 'ERR' AND @Function != 'wed')

    OR
      (@RESULT = 'OK' AND @FUNCTION = 'fre' AND (@ADVISORY != '' OR @ADVISORY != 'SECURITY SUCCESSFULLY PROCESSED' OR @ADVISORY = 'REQUESTED Message DOES NOT EXIST'))

    OR
       (@RESULT = 'OK' AND @FUNCTION = 'Abh' AND @ADVISORY = 'APPLICATION NOT READY')

BEGIN

                  DECLARE @AutoReplay BIT;     

                  DECLARE @ErrorCode INT;

                  DECLARE @ErrorDescription VARCHAR(255);

                  DECLARE @NewId AS INT

               IF (@ADVISORY IS NOT NULL)

                        BEGIN  

                           SELECT   @AutoReplay = AutoReplay,

                                          @ErrorCode = ErrorCode

                              FROM PErrors

                              WHERE ErrorDescription = @Advisory

                              IF @ErrorCode IS NULL

                              BEGIN      

                                    INSERT PErrors(ErrorDescription, AutoReplay)

                                    Values(@Advisory, 0)

                                    SELECT @NewId = @@IDENTITY;

                                    INSERT PErrorDetails(ErrorCode,ApplicationID, SP)

                                    Values(@NewId, 1, NULL)

                                    INSERT PErrorDetails(ErrorCode,ApplicationID, SP)

                                    Values(@NewId, 2, NULL)                        

                              END
                     END

                    IF(@Advisory = 'INVALID ACCOUNT NUMBER' OR @ADVISORY = 'CANNOT CANCEL A CANCELLED TRADE')

                        RETURN;

                    DECLARE @lastMsgIDStamp INT;

                    DECLARE @currentMsgRetrievalMin INT;

                    DECLARE @severity INT;

                    DECLARE @defaultTimeInterval INT;

                    DECLARE @accountType INT;

                    DECLARE @storedProc VARCHAR(30);

                    DECLARE @btMsgIDCount INT;

                    DECLARE @increment BIT;

                    DECLARE @btMsgID1 uniqueidentifier;                 

                    SET @currentMsgRetrievalMin  = 5;

                    SET @increment = 0;

                    SET @accountType = 1;       

                     IF EXISTS(SELECT * FROM PERRORS WHERE ErrorDescription = @Advisory)

                              BEGIN

                                       SELECT @severity = errorSeverity,

                                                        @errorCode = errorCode,

                                                        @defaultTimeInterval = timeInterval,

                                      @AutoReplay = AutoReplay  

                                                   FROM  PHASE3ERRORS

                                                   WHERE ErrorDescription IN (@Advisory);                                             

                               END 

                   SELECT @btMsgIDCount = COUNT(BTMSGID) FROM PRESULT WHERE BTMSGID = @btMsgID

                     IF(@btMsgIDCount = 10  OR @btMsgIDCount = 20 OR @btMsgIDCount = 30 OR @btMsgIDCount = 40)

                     BEGIN

                          SET @increment = 1;  

                     END

                        DECLARE @btMsgIdVar varchar(128);

                        SET @btMsgIdVar = rtrim(@btMsgId);     

            IF EXISTS(SELECT btMsgID FROM SEnrichment WHERE BTMsgid = @btMsgIdVar)

         BEGIN

                        SELECT @storedProc = SP FROM PErrorDetails AS PE INNER JOIN [APPLICATIONS] AS A

                        ON PE.ApplicationID = A.ApplicationID AND ErrorCode = @errorCode

                        AND AppName = 'Fast'

                        IF(@storedProc IS NOT NULL)

                              BEGIN  

                                Exec @storedProc @btMsgIdVar;

                              END        

                 

                  IF @AutoReplay = 0   

                       RETURN;    

                         SELECT TOP 1 @lastMsgIDStamp = DATEDIFF(mi,Stamp,GetDate()),

                         @currentMsgRetrievalMin = timeInterval FROM SReplay

                         WHERE btMsgId = @btMsgId ORDER BY STAMP DESC 

                  IF (@increment = 1)

                           BEGIN

                                SET @currentMsgRetrievalMin = @currentMsgRetrievalMin + 10;

                              END   

                        INSERT SReplay

                        (

                              btmsgid,

                              Stamp,

                              Function,

                              Processed,

                              TimeInterval

                        )

                        Values

                        (    

                        @btMsgId,

                              getdate(),

                              @Function,

                              0,

                              @currentMsgRetrievalMin

                        )

          END       

      IF EXISTS(SELECT * FROM Universal_Persisted WHERE btMsgId = rtrim(@btMsgId))

      BEGIN

        SELECT @storedProc = SP FROM PErrorDetails PE, [APPLICATIONS] A

            WHERE PE.ApplicationID = A.ApplicationID AND ErrorCode = @errorCode

        AND AppName = 'TRADE'

              IF(@storedProc IS NOT NULL)

                BEGIN  

              Exec @storedProc @btMsgIdVar;

                  END

           IF @AutoReplay = 0   

              RETURN;

     

           SELECT TOP 1 @lastMsgIDStamp = DATEDIFF(mi,Stamp,GetDate()),

            @currentMsgRetrievalMin = timeInterval FROM TREPLAY

            WHERE btMsgId = @btMsgId ORDER BY STAMP DESC  

                  IF (@increment = 1)

                     BEGIN

                          SET @currentMsgRetrievalMin = @currentMsgRetrievalMin + 10;

                END
                        INSERT TReplay

                              (

                                    btmsgid,

                                    Stamp,

                                    Function,

                                    Processed,

                                    TimeInterval

                              )

                              Values

                              (    

                                    @btMsgId,

                                    getdate(),

                                    @Function,

                                    0,

                                    @currentMsgRetrievalMin

                              )

      END

END


Viewing all articles
Browse latest Browse all 7129

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>