Help to optimize SP

  • ALTER PROCEDURE [dbo].[PMLline_sp]

    (

    @Requestnovarchar(10),

    @GroupNamevarchar(50), -- BO governor role

    @useridvarchar(10),

    @DteTimedatetime,

    @BONamevarchar(10), -- current bo group DTRGRPOPT

    @Remarksvarchar(2000),

    @MDTType varchar(10),-- type of request

    @StartDatedatetime,-- previous history date

    @Assignbyvarchar(10),-- current user

    @RequestStatus varchar(10),-- CMS request status

    @SiteAccess int,--value 1/0

    @RDCvarchar(20),--RDC name (west,south..)

    @RDCStatusvarchar(10)--status code of RDC

    )

    AS

    BEGIN

    DECLARE @HistoryStatusvarchar(10)

    -- Insert Record of Company Group

    INSERT INTO BOGOV

    VALUES (@Requestno,@GroupName,@UserId,@DteTime,0,'')

    -- Select status of history depending on the user

    SELECT distinct @HistoryStatus =grpcode from nnccmnocmdtgroup

    WHERE GrpRole = @GroupName

    -- Insert history of Company actionINSERT INTO comhistorylog

    VALUES( @RequestNo,

    (SELECT MAX(coalesce(historyorder,0))+ 1 from comhistorylog where RequestNo = @RequestNo), <-- getting the max record manully.

    @MDTType,@StartDate,@Assignby,@RequestStatus,@HistoryStatus,0,@Remarks,GETDATE())

    -- Since this is a transaction I need to delete the record from the temp table

    -- it means action has been taken and record is in the next step

    -- executing a stored procedure

    exec DeleteGroup_sp @Requestno,@BOName

    DECLARE @StillValue int

    CREATE TABLE #TempExists

    (

    Ivalue int

    )

    -- selecting the table if there are still rows from the group table

    -- rows can be upto 5, record is deleted when once user initiate an action.

    -- it will execute the above SP "DeleteGroup_sp"

    -- insert into temp table for checking if there are still rows.

    INSERT INTO #TempExists

    exec ReadGroup_sp @Requestno

    -- this is where I have problem, when the database server is slow due to a lot of reason which I don't have control.

    -- it fails on this part.

    -- if there is no record it should execute the remaining INSERT statement to complete the process.

    -- what happened is that it doesn't execute the below statement and there is discrepancy.

    -- any suggestion to optimize my SP?

    -- will locking work or will make it worst.

    -- we have 50 users using this process on a 24/7 shift.

    SET @StillValue = (select Ivalue from #TempExists)

    IF (@StillValue =0)

    BEGIN

    -- execute to update the record to move to next group

    exec ReviseRequest_sp @Requestno,@UserId,'47'

    --START INSERT RECORD FOR BOSS GOV

    IF NOT EXISTS (select * from BOGOV WHERE Requestno = @Requestno AND GroupName = 'MGR')

    BEGIN

    INSERT INTO BOGOV

    VALUES (@Requestno,'DTRMGT',@UserId,@DteTime,0,'')

    INSERT INTO comhistorylog

    VALUES( @RequestNo,

    (SELECT MAX(coalesce(historyorder,0))+ 1 from comhistorylog where RequestNo = @RequestNo), <-- getting the max record manully.

    @MDTType,@StartDate,@Assignby,@RequestStatus,'17',0,@Remarks,GETDATE())

    END

    IF @SiteAccess = 1

    BEGIN

    IF NOT EXISTS (select * from BOGOV WHERE Requestno = @Requestno AND GroupName = @RDC)

    BEGIN

    INSERT INTO BOGOV

    VALUES (@Requestno,@RDC,@UserId,@DteTime,0,'')

    INSERT INTO comhistorylog

    VALUES(@RequestNo,(SELECT MAX(coalesce(historyorder,0))+ 1 from comhistorylog where RequestNo = @RequestNo), <-- getting the max record manully.

    @MDTType,@StartDate,@Assignby,@RequestStatus,@RDCStatus,0,@Remarks,GETDATE())

    END

    END

    --enable the record fr om nnccmdtgov table from 0 to 1

    exec EnableMNOCBOGOV_sp@Requestno

    END

    END

    Thanks;

  • Nice procedure. So what do you expect us to do with it?

    Please post table definitions, index definitions and execution plan, as per http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • How can I handle wherein when one insert fails or select statement fails due to server slow

    the rest of the SP will fails.

    Or can I increase the timeout expired to handle waiting period when executing?

    The main table that I am using is referrence from different system since it is a transaction table.

    While I am doing transaction on the main table other appllication is also accessing the same table

    as reference.

    Regards

  • Timeout is a client-side setting, not a server side. It means the client has got tired of waiting and told SQL to cancel the execution.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • yes but what is your suggestion on my first scenario?

  • An insert or select won't fail because the server's slow. SQL by itself doesn't care how long a query takes. If the procedure is timing out, then it means that the client app has decided that the proc took too long and has told SQL to terminate it.

    How you get around that is my optimising your procedure so it doesn't take so long or changing the timeout in the client application.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • yes I stand corrected not insert and update it is the update that failes.

    exec ReviseRequest_sp @Requestno,@UserId,'47'

    in the procedure above a straight forward one fails becuase of hanging transaction on the table itself.

    when the update failed it doesn't go thru the succeding actions in SP.

    This is the best SP for this scenario but to handle when the update fails the rest of the SP should fail also,

    insert in the beginning should roll back..

    Thanks

  • Update won't fail because it takes too long either, neither will a delete.

    If you have the update failing and you want the insert to be rolled back, then you need a transaction and error handling. Look up Begin transaction and Try... Catch.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • ok thanks.

    I will add try catch and extend the sqltimeout from the client.

    Regards

  • If you have the update failing and you want the insert to be rolled back, then you need a transaction and error handling. Look up Begin transaction and Try... Catch.

    Not just error handling.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • hey you can use sql profiler for this.

    then u will come to know that which thread taking more time to execute.

    after that check with developer and do needful changes.

Viewing 11 posts - 1 through 10 (of 10 total)

You must be logged in to reply to this topic. Login to reply