Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

Help to optimize SP Expand / Collapse
Author
Message
Posted Tuesday, December 11, 2012 10:06 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Tuesday, August 20, 2013 6:14 AM
Points: 18, 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;
Post #1395422
Posted Tuesday, December 11, 2012 11:42 PM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Yesterday @ 1:43 PM
Points: 40,411, Visits: 36,861
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 2008, MVP
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

Post #1395447
Posted Wednesday, December 12, 2012 12:49 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Tuesday, August 20, 2013 6:14 AM
Points: 18, 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

Post #1395475
Posted Wednesday, December 12, 2012 12:54 AM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Yesterday @ 1:43 PM
Points: 40,411, Visits: 36,861
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 2008, MVP
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

Post #1395477
Posted Wednesday, December 12, 2012 12:57 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Tuesday, August 20, 2013 6:14 AM
Points: 18, Visits: 52
yes but what is your suggestion on my first scenario?
Post #1395479
Posted Wednesday, December 12, 2012 1:02 AM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Yesterday @ 1:43 PM
Points: 40,411, Visits: 36,861
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 2008, MVP
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

Post #1395482
Posted Wednesday, December 12, 2012 1:09 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Tuesday, August 20, 2013 6:14 AM
Points: 18, 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
Post #1395484
Posted Wednesday, December 12, 2012 1:17 AM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Yesterday @ 1:43 PM
Points: 40,411, Visits: 36,861
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 2008, MVP
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

Post #1395485
Posted Wednesday, December 12, 2012 1:24 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Tuesday, August 20, 2013 6:14 AM
Points: 18, Visits: 52
ok thanks.
I will add try catch and extend the sqltimeout from the client.

Regards
Post #1395487
Posted Wednesday, December 12, 2012 2:01 AM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Yesterday @ 1:43 PM
Points: 40,411, Visits: 36,861
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 2008, MVP
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

Post #1395507
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse