Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server (TSQL) - Is it possible to EXEC statements in parallel?

SQL Server 2008 R2

Here is a simplified example:

EXECUTE sp_executesql N'PRINT ''1st '' + convert(varchar, getdate(), 126) WAITFOR DELAY ''000:00:10'''
EXECUTE sp_executesql N'PRINT ''2nd '' + convert(varchar, getdate(), 126)'

The first statement will print the date and delay 10 seconds before proceeding. The second statement should print immediately.

The way T-SQL works, the 2nd statement won't be evaluated until the first completes. If I copy and paste it to a new query window, it will execute immediately.

The issue is that I have other, more complex things going on, with variables that need to be passed to both procedures.

What I am trying to do is:

  • Get a record
  • Lock it for a period of time
  • while it is locked, execute some other statements against this record and the table itself

Perhaps there is a way to dynamically create a couple of jobs?

Anyway, I am looking for a simple way to do this without having to manually PRINT statements and copy/paste to another session.

Is there a way to EXEC without wait / in parallel?

like image 735
Outside the Box Developer Avatar asked Dec 31 '10 18:12

Outside the Box Developer


People also ask

Can SQL queries run in parallel?

SQL Server has the ability to execute queries using multiple CPUs simultaneously. We refer to this capability as parallel query execution. Parallel query execution can be used to reduce the response time of (i.e., speed up) a large query.

Can we execute queries parallel from different session?

In general the answer to your question is "yes".

Can I run multiple SQL queries at once?

You can include multiple SQL statements on the SQL query panel. The exceptions are CALL and CREATE PROCEDURE statements. These statements must be used alone in a query.


Video Answer


3 Answers

Yes, there is a way, see Asynchronous procedure execution.

However, chances are this is not what you need. T-SQL is a data access language, and when you take into consideration transactions, locking and commit/rollback semantics is almost impossible to have a parallel job. Parallel T-SQL works for instance with requests queues, where each requests is independent and there is no correlation between jobs.

What you describe doesn't sound at all like something that can, nor should, actually be paralellized.

like image 167
Remus Rusanu Avatar answered Sep 21 '22 03:09

Remus Rusanu


If you want to lock a record so you can execute statements against it, you may want to execute those statements as a transaction.

To execute SQL in parallel, you need to paralellize SQL calls, by executing your SQL from within separate threads/processes in Java, C++, perl, or any other programming language (hell, launching "isql" in shell script in background will work)

like image 28
DVK Avatar answered Sep 25 '22 03:09

DVK


If after reading all above about potential problems and you still want to run things in parallel, you probably can try sql jobs, put your queries in different jobs, then execute by calling the jobs like this

EXEC msdb..sp_start_job 'Job1'

EXEC msdb..sp_start_job 'Job2' 
like image 41
robotj Avatar answered Sep 21 '22 03:09

robotj