4

Is there any way to run several SELECT statements concurrently inside a Stored Procedure in SQL Server 2008 (parallel execution)?

If so, what steps must be followed?

edgarmtze
  • 24,683
  • 80
  • 235
  • 386

5 Answers5

2

If you mean can you control it, the answer is no.

However, if you have multiple selects where the results of those selects don't affect each other then the query optimiser will normally parallelise them for you, with provisios on cpu ability.

You can see this when you view the query plan;

Parallel Query Plan

but you won't see this in Sql Express, or any of the developer editions, only on full server editions.

blowdart
  • 55,577
  • 12
  • 114
  • 149
1

There are no steps you can take:

  • multiple DML statements in one batch do not run concurrently

  • you can not switch this on

  • parallelism will be chosen by the optimiser based on query, indexes, cost etc: you can't switch it on

If you have 50 SELECTs I'd suggest you're doing something wrong. Why don't you ask about your real problem: instead of what you perceive the solution to your problem to be...

gbn
  • 422,506
  • 82
  • 585
  • 676
  • I need to insert to different tables, that is the reason I have several select, Or is it a way to add a switch statement inside a select? – edgarmtze May 12 '11 at 13:47
0

Not very clear your scenario, maybe you could use 'UNION' on several SELECT. or Temp tables to keep the intermidiate results.

mothee
  • 141
  • 2
  • 4
  • 12
0

Agree with @blowdart completely - SQL will normally optimise the degree of parallelism for you.

The only thing that you can do is limit that degree of parallelism, with the MAXDOP option (MAXimum Degree Of Parallelism).

SELECT [Id] FROM [Table] OPTION (MAXDOP 1)

See also:

http://support.microsoft.com/kb/329204

What is the purpose for using OPTION(MAXDOP 1) in SQL Server?

Community
  • 1
  • 1
Kirk Broadhurst
  • 27,836
  • 16
  • 104
  • 169
  • Excuse, so if I have 50 Selects I add this code on every of them, And depending the cores I get a faster execution?. – edgarmtze May 12 '11 at 04:39
0

You could use the service broker to run things in parallel. I have done this where I had to update or insert many rows into 15 different tables. I set the update/insert statements onto the service broker queue and let it run 10 way parallel.

There is some overhead in the queuing, but the inserts/updates are large enough - it can be beneficial.

JoeLeBaron
  • 338
  • 1
  • 3
  • 12