Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I run sql server stored procedures in parallel?

I want to do something like:

exec sproc1 and sproc2 at the same time
when they are both finished exec sproc3

I can do this in dts. Is there a way to do it in transact sql? Or is there a way to do it with a batch script (eg vbs or powershell)?

like image 682
cindi Avatar asked Nov 10 '08 13:11

cindi


3 Answers

You could create a CLR Stored Procedure that (using C#) would call the first two on their own threads, and then block until both are complete... then run the third one.

Are you able to use CLR sprocs in your situation? If so, I'll edit this answer to have more detail.

like image 102
Timothy Khouri Avatar answered Sep 22 '22 12:09

Timothy Khouri


sp _ start _ job

I'm doing a similar thing at the moment, and the only way I've found to avoid using SSIS or some external shell is to split my load routine into 'threads' manually, and then fire a single master sqlagent job which in turn executes as many sp _ start _ job's as I have threads. From that point, they all run autonomously.

It's not exactly what we're looking for, but the result is the same. If you test the job status for the sub jobs, you can implement your conditional start of sproc 3 as well.

What's the point in 8 cores if we can't use them all at once?

like image 44
Grokling Avatar answered Sep 22 '22 12:09

Grokling


Do you absolutely need both SPs to be executed in parallel?

With simple CRUD statements within a single SP, I've found SQL S. does a very good job of determining which of them can be run in parallel and do so. I've never seen SQL S. run 2 SPs in parallel if both are called sequentially from a T-SQL statement, don't even know if it's even possible.

Now then, do the DTS really execute them in parallel? It could be it simply executes them sequentially, then calls the 3rd SP after the last finishes successfully.

If it really runs them in parallel, probably you should stick with DTS, but then I'd like to know what it does if I have a DTS package call, say, 10 heavy duty SPs in parallel... I may have to do some testings to learn that myself :D

like image 33
Joe Pineda Avatar answered Sep 22 '22 12:09

Joe Pineda