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


Execute T-SQL Scripts in Parallel


Execute T-SQL Scripts in Parallel

Author
Message
RBarryYoung
RBarryYoung
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14686 Visits: 9518
RBarryYoung (8/31/2009)
Where is sp_exec_init documented?


Still waiting for an answer ... ?

-- RBarryYoung, (302)375-0451 blog: MovingSQL.com, Twitter: @RBarryYoung
Proactive Performance Solutions, Inc.
"Performance is our middle name."
jmsma2002
jmsma2002
SSC-Enthusiastic
SSC-Enthusiastic (118 reputation)SSC-Enthusiastic (118 reputation)SSC-Enthusiastic (118 reputation)SSC-Enthusiastic (118 reputation)SSC-Enthusiastic (118 reputation)SSC-Enthusiastic (118 reputation)SSC-Enthusiastic (118 reputation)SSC-Enthusiastic (118 reputation)

Group: General Forum Members
Points: 118 Visits: 289
RBarryYoung (9/1/2009)
RBarryYoung (8/31/2009)
Where is sp_exec_init documented?


Still waiting for an answer ... ?


Sorry, I haven't written a lot document yet. Do you only want to know how to call it? It's like:

EXEC dbo.sp_exec_init [@worker_num=4]

The @worker_num means how many worker processes you want to run your sql's. It's optional argument.

Indeed that's the wrapper of this stored procedure in the pmaster database. The code is not long or fancy, I think you guys can figure it out. Forgive me this time since my work load is heavy at present.

CREATE procedure [dbo].[p_exec_init]
@worker_num smallint = 4
as
......



Wilfred van Dijk
Wilfred van Dijk
Ten Centuries
Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)

Group: General Forum Members
Points: 1405 Visits: 1359
Excellent piece of code! I implemented it to speed-up some Datawarehouse processing routines. Processing time dropped from 500 minutes to 70 minutes! w00t w00t w00t

Wilfred
The best things in life are the simple things
Wilfred van Dijk
Wilfred van Dijk
Ten Centuries
Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)

Group: General Forum Members
Points: 1405 Visits: 1359
Nested tasks not possible?
Situation: from a main routine, I execute a subroutine with sp_exec. inside this subroutine, some calculations are done (a generated SQL statment). If I execute this SQL statement with sp_exec it seems it's not executed at all. no error message either. Can't find the reason. (SQLStatement is ok, checked)


exec mainroutine
-- in loop: sp_exec subroutine
-- in loop: sp_exec sql statement



Question: can you add another process from within a parallel process?

Wilfred
The best things in life are the simple things
jmsma2002
jmsma2002
SSC-Enthusiastic
SSC-Enthusiastic (118 reputation)SSC-Enthusiastic (118 reputation)SSC-Enthusiastic (118 reputation)SSC-Enthusiastic (118 reputation)SSC-Enthusiastic (118 reputation)SSC-Enthusiastic (118 reputation)SSC-Enthusiastic (118 reputation)SSC-Enthusiastic (118 reputation)

Group: General Forum Members
Points: 118 Visits: 289
If you simply sp_exec in loops, notice still they (many worker spids) are all controlled by current master SPID and in theory they will all run in the same parallel scope. However, you can use sp_exec_wait to control the running sequence between sql groups. Like the following pseudo code:


sp_exec_init

loop begin
sp_exec '...'
sp_exec '...'

loop begin
sp_exec '...'
loop end
sp_exec_wait

sp_exec '...'
sp_exec '...'

loop end

sp_exec_end




If you really want to start other parallel scopes within current parallel process, the following pseudo code shows the trick. You can launch a worker spid to run the sp_exec script.


--This is in current master process
sp_exec_init

--This will launch a worker process who will master a bunch of worker spids
sp_exec '
sp_exec_init
sp_exec ''...''
sp_exec ''...''
sp_exec_wait
sp_exec_end
'

--This will launch another worker process who will master a bunch of worker spids
sp_exec '
sp_exec_init
sp_exec ''...''
sp_exec ''...''
sp_exec_wait
sp_exec_end
'
sp_exec_wait
sp_exec_end




Anytime you can look at pmaster.dbo.exec_queue table to debug the running of your script.



Robb Melancon
Robb Melancon
SSC-Enthusiastic
SSC-Enthusiastic (103 reputation)SSC-Enthusiastic (103 reputation)SSC-Enthusiastic (103 reputation)SSC-Enthusiastic (103 reputation)SSC-Enthusiastic (103 reputation)SSC-Enthusiastic (103 reputation)SSC-Enthusiastic (103 reputation)SSC-Enthusiastic (103 reputation)

Group: General Forum Members
Points: 103 Visits: 110
I like the tool, however is there anyway to have it return rows? I have a need to execute multiple sprocs in parallel but these procs return result sets for a report.

Thanks for your time,
Robb
jmsma2002
jmsma2002
SSC-Enthusiastic
SSC-Enthusiastic (118 reputation)SSC-Enthusiastic (118 reputation)SSC-Enthusiastic (118 reputation)SSC-Enthusiastic (118 reputation)SSC-Enthusiastic (118 reputation)SSC-Enthusiastic (118 reputation)SSC-Enthusiastic (118 reputation)SSC-Enthusiastic (118 reputation)

Group: General Forum Members
Points: 118 Visits: 289
Robb Melancon (5/7/2010)
I like the tool, however is there anyway to have it return rows? I have a need to execute multiple sprocs in parallel but these procs return result sets for a report.

Thanks for your time,
Robb



The method I've been using is to create a table in tempdb (don't use those like #tmp but use a real table like tempdb.dbo.tmp_result), and let all the parallel runing scripts insert into the table. Then you can collect combined results after all sprocs return.



Robb Melancon
Robb Melancon
SSC-Enthusiastic
SSC-Enthusiastic (103 reputation)SSC-Enthusiastic (103 reputation)SSC-Enthusiastic (103 reputation)SSC-Enthusiastic (103 reputation)SSC-Enthusiastic (103 reputation)SSC-Enthusiastic (103 reputation)SSC-Enthusiastic (103 reputation)SSC-Enthusiastic (103 reputation)

Group: General Forum Members
Points: 103 Visits: 110
Thanks for the reply. The only problem with using temps is that they would need to be global or declared directly in temp database like you said. This is fine for single user but when you have multiple users running reports I'd need to create a guid per each run of the report and filter on the guid. There will be other problem potentially with this like locking for inserts while some other user is running a report etc. It may be the only solution though. Another thing I thought would be passing the table as a parameter but this may have some performance issues. Anyway, thanks again for the reply.
jmsma2002
jmsma2002
SSC-Enthusiastic
SSC-Enthusiastic (118 reputation)SSC-Enthusiastic (118 reputation)SSC-Enthusiastic (118 reputation)SSC-Enthusiastic (118 reputation)SSC-Enthusiastic (118 reputation)SSC-Enthusiastic (118 reputation)SSC-Enthusiastic (118 reputation)SSC-Enthusiastic (118 reputation)

Group: General Forum Members
Points: 118 Visits: 289
Robb Melancon (5/10/2010)
Thanks for the reply. The only problem with using temps is that they would need to be global or declared directly in temp database like you said. This is fine for single user but when you have multiple users running reports I'd need to create a guid per each run of the report and filter on the guid. There will be other problem potentially with this like locking for inserts while some other user is running a report etc. It may be the only solution though. Another thing I thought would be passing the table as a parameter but this may have some performance issues. Anyway, thanks again for the reply.


In that case, perhaps you can create seperate temp tables for each users instead of filtering on a guid column, like:

tmp_5B12952D614E497A93F7EA670B279A75
tmp_5C0EDDEFE61740EDAB5A0DDBC67088F0


I think that won't require filtering on guid and won't have locking issues. Only need to optimize tempdb a bit or even creating a user database as the temp db.



gsc_dba
gsc_dba
SSCommitted
SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)

Group: General Forum Members
Points: 1756 Visits: 2019
Wilfred van Dijk (3/19/2010)
Excellent piece of code! I implemented it to speed-up some Datawarehouse processing routines. Processing time dropped from 500 minutes to 70 minutes! w00t w00t w00t


I am intrigued how you achieved this, can you please give a brief example...

After some subtle changes in our ETL processes I can now see how!

_____________________________________________________________________________________
[font=Courier New]gsc_dba[/font]
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