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

store proc based logical issue Expand / Collapse
Author
Message
Posted Friday, September 13, 2013 6:12 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Wednesday, March 12, 2014 12:18 PM
Points: 96, Visits: 347
I have a job which runs a store procedure. The proc is something like
BEGIN

STEP 1) Archive records into Table A..... (around 15K records)
from TABLE B

STEP 2) Delete some records from the table B

STEP 3) Insert new records into Table B

STEP 4) Update table B

END



This proc takes about 20 mins to run.
Jobs kicks at 7:15 and as soon as job starts i can not access records from TABLE B deleted in STEP 2. I know step 1 takes about 10 mins . My assumption was that SP has sequential processing and not parallel processing. Is it possible that it can run statement 2 along with step 1. I am confused about how it gets executed. I cant see execution plan and getting permission is like 10 day process.
Post #1494554
Posted Friday, September 13, 2013 8:59 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 12:33 PM
Points: 14,800, Visits: 27,274
Since that's all going to be part of a single transaction, you're very likely going to see all sorts of blocking, not just at the row level, but possibly at the page level, preventing you from accessing data in Table B while the transaction completes. If you need to access data while it's being run, you might want to look at setting up a snapshot isolation level on the server.

----------------------------------------------------
"The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood..." Theodore Roosevelt
The Scary DBA
Author of: SQL Server 2012 Query Performance Tuning
SQL Server 2008 Query Performance Tuning Distilled
and
SQL Server Execution Plans

Product Evangelist for Red Gate Software
Post #1494603
Posted Friday, September 13, 2013 9:13 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 12:21 PM
Points: 11,985, Visits: 11,004
In addition to Grant's suggestion maybe you should look at Step1. You say there are only about 15k rows but the archive portion takes 10 minutes. That is awfully slow for not many rows. Are the other 3 steps fast? I would think that archiving 15k rows should take 10-15 seconds tops.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1494616
Posted Friday, September 13, 2013 9:27 AM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: 2 days ago @ 8:43 PM
Points: 4,128, Visits: 5,836
ekant_alone (9/13/2013)
I have a job which runs a store procedure. The proc is something like
BEGIN

STEP 1) Archive records into Table A..... (around 15K records)
from TABLE B

STEP 2) Delete some records from the table B

STEP 3) Insert new records into Table B

STEP 4) Update table B

END


This proc takes about 20 mins to run.
Jobs kicks at 7:15 and as soon as job starts i can not access records from TABLE B deleted in STEP 2. I know step 1 takes about 10 mins . My assumption was that SP has sequential processing and not parallel processing. Is it possible that it can run statement 2 along with step 1. I am confused about how it gets executed. I cant see execution plan and getting permission is like 10 day process.


1) Try sp_whoisactive as soon as you kick off the sproc to look for locking blocking. GREAT free resource found on sqlblog.com. You may not have permission to run this either.

2) The statements in a proc do not EVER execute at the same time. They are serial as you expect.

3) Can you see the ESTIMATED execution plan? That could be good enough to identify that you are doing a nasty table scan or something silly to delete just a few rows or for other parts of the sproc.

4) The WITH (ROWLOCK) hint could be helpful. See Books Online for info.

5) I question the order of steps 3 and 4. Assuming your INSERT already has all the correct information for every row, doing the INSERT before the UPDATE simply puts more rows in the table to subsequently be UPDATED. Not efficient coding practices (although rather common at clients I go to!).



Best,

Kevin G. Boles
SQL Server Consultant
SQL MVP 2007-2012
TheSQLGuru at GMail
Post #1494626
Posted Friday, September 13, 2013 10:26 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: Today @ 12:20 PM
Points: 41,529, Visits: 34,445
TheSQLGuru (9/13/2013)
4) The WITH (ROWLOCK) hint could be helpful. See Books Online for info.


Or could make it worse. 15000 rows locked at the row level is above the threshold for triggering an escalation to table-level locks.



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 #1494654
Posted Friday, September 13, 2013 10:28 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: Today @ 12:20 PM
Points: 41,529, Visits: 34,445
ekant_alone (9/13/2013)
My assumption was that SP has sequential processing and not parallel processing. Is it possible that it can run statement 2 along with step 1.


Your assumption is correct, statements in a stored procedure execute in sequence. Step 2 won't start until step 1 completes. Step 1 will lock rows in TableB though. Should just be shared locks, but they will be locked.



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 #1494657
Posted Saturday, September 14, 2013 7:17 AM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: 2 days ago @ 8:43 PM
Points: 4,128, Visits: 5,836
GilaMonster (9/13/2013)
TheSQLGuru (9/13/2013)
4) The WITH (ROWLOCK) hint could be helpful. See Books Online for info.


Or could make it worse. 15000 rows locked at the row level is above the threshold for triggering an escalation to table-level locks.


True. But if it is a busy table there will almost certainly be other row/page locks that will prevent the table lock escalation, thus allowing the row locking to continue (up to memory limitations IIRC) in 1250-lock increments, where it will try for the table lock again iteratively. Other than memory concerns this will probably be a better concurrency scenario in many cases than simply attempting to get table lock at the start (and possibly being blocked for long periods) or actually GETTING the table lock at the start and blocking other activity for duration of the transaction.

YMMV, and testing is important as it always is when you use hints!


Best,

Kevin G. Boles
SQL Server Consultant
SQL MVP 2007-2012
TheSQLGuru at GMail
Post #1494807
Posted Friday, November 01, 2013 1:42 PM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Wednesday, December 04, 2013 1:49 PM
Points: 1,104, Visits: 1,174
OP,

I am just curious ... how do you know step 1 takes 10 minutes? Is there some way that you actually know that or is it an assumption based on something else?

If you can read from table B, but just not the records expected to be deleted, that would indicate that your time estimates are off and that the step in question has already been completed. As has been stated, the 2nd statement will not execute until the 1st has completed. If you can't read from table B in general because it is locked that is a separate issue.

Personally I like using SSIS for ETL tasks like you're describing rather than using single procs that perform many separate tasks. For each separate action I use a distinct step and if I have any issues I can easily discover exactly how long each step is taking and where the errors are. Some of this advantage could be had merely by separating the statements into separate procs and then calling the procs in order via the job. Of course, there are times when the code needs to be kept together and your case could very well be one of those times.


└> bt


Forum Etiquette: How to post data/code on a forum to get the best help
Post #1510793
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse