SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Help to optimize SP


Help to optimize SP

Author
Message
SqlUser-369680
SqlUser-369680
SSC Rookie
SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)

Group: General Forum Members
Points: 30 Visits: 52
ALTER PROCEDURE [dbo].[PMLline_sp]
(
@Requestno varchar(10),
@GroupName varchar(50), -- BO governor role
@UserId varchar(10),
@DteTime datetime,
@BOName varchar(10), -- current bo group DTRGRPOPT
@Remarks varchar(2000),

@MDTType varchar(10), -- type of request
@StartDate datetime, -- previous history date
@Assignby varchar(10), -- current user
@RequestStatus varchar(10), -- CMS request status
@SiteAccess int, --value 1/0
@RDC varchar(20), --RDC name (west,south..)
@RDCStatus varchar(10) --status code of RDC
)
AS

BEGIN
DECLARE @HistoryStatus varchar(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 action 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,@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;
GilaMonster
GilaMonster
SSC Guru
SSC Guru (116K reputation)SSC Guru (116K reputation)SSC Guru (116K reputation)SSC Guru (116K reputation)SSC Guru (116K reputation)SSC Guru (116K reputation)SSC Guru (116K reputation)SSC Guru (116K reputation)

Group: General Forum Members
Points: 116949 Visits: 45530
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


SqlUser-369680
SqlUser-369680
SSC Rookie
SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)

Group: General Forum Members
Points: 30 Visits: 52
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
GilaMonster
GilaMonster
SSC Guru
SSC Guru (116K reputation)SSC Guru (116K reputation)SSC Guru (116K reputation)SSC Guru (116K reputation)SSC Guru (116K reputation)SSC Guru (116K reputation)SSC Guru (116K reputation)SSC Guru (116K reputation)

Group: General Forum Members
Points: 116949 Visits: 45530
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


SqlUser-369680
SqlUser-369680
SSC Rookie
SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)

Group: General Forum Members
Points: 30 Visits: 52
yes but what is your suggestion on my first scenario?
GilaMonster
GilaMonster
SSC Guru
SSC Guru (116K reputation)SSC Guru (116K reputation)SSC Guru (116K reputation)SSC Guru (116K reputation)SSC Guru (116K reputation)SSC Guru (116K reputation)SSC Guru (116K reputation)SSC Guru (116K reputation)

Group: General Forum Members
Points: 116949 Visits: 45530
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


SqlUser-369680
SqlUser-369680
SSC Rookie
SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)

Group: General Forum Members
Points: 30 Visits: 52
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
GilaMonster
GilaMonster
SSC Guru
SSC Guru (116K reputation)SSC Guru (116K reputation)SSC Guru (116K reputation)SSC Guru (116K reputation)SSC Guru (116K reputation)SSC Guru (116K reputation)SSC Guru (116K reputation)SSC Guru (116K reputation)

Group: General Forum Members
Points: 116949 Visits: 45530
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


SqlUser-369680
SqlUser-369680
SSC Rookie
SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)

Group: General Forum Members
Points: 30 Visits: 52
ok thanks.
I will add try catch and extend the sqltimeout from the client.

Regards
GilaMonster
GilaMonster
SSC Guru
SSC Guru (116K reputation)SSC Guru (116K reputation)SSC Guru (116K reputation)SSC Guru (116K reputation)SSC Guru (116K reputation)SSC Guru (116K reputation)SSC Guru (116K reputation)SSC Guru (116K reputation)

Group: General Forum Members
Points: 116949 Visits: 45530
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


Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search