0

Not a dupe of any of these:

Since those relate to how to execute parallel queries from particular language. To which the answer is naturally "just fire off the queries in parallel, in whatever manner your language has for parallel execution".

One answer on the above comments that one way to have 2 processes, is that you can open 2 SSMS instances, or tabs, and run those queries in parallel. That's also not what I'm asking.


I want to know if there's any way to write SQL in a single SSMS tab, such that with a single execution (i.e. pressing F5 once) I can run 2 queries in parallel?

By analogy, how can I run a query 5 times, in a single SSMS tab, without copy-pasting it? Would either get the answer: "Look into the SQL WHILE loop", or "use GO 5". Not "here's how your write a for loop in C#".

Brondahl
  • 7,402
  • 5
  • 45
  • 74
  • SQL is run sequentially. If you have 2 statements in a single batch then they will be run in sequence. You need 2 connections to run 2 statements simultaneously, 3 connection for 3, etc. – Thom A Sep 03 '20 at 16:26
  • Note you *can* run the same query against multiple **instances** simultaneously using registered servers. – Thom A Sep 03 '20 at 16:27
  • @Larnu If you also want to assert executing text from a SSMS window always uses a single connection then I'd happily accept that as an answer. (Especially if you have anything you can link to about it) Is that the case? – Brondahl Sep 03 '20 at 16:30
  • The nagging question for me is why? What are you trying to accomplish that requires queries to be run in parallel in SSMS? – Sean Lange Sep 03 '20 at 16:54
  • :) I want to compare the performance of 1 (quick) query which returns all the data in one go, versus 3 quick queries which return 3 fragments of data ... but would be executed in parallel. The impact of the queries running *in parallel* could potentially be significant, so it's insufficient to just run them in series and hope that the results are the same when run in parallel – Brondahl Sep 03 '20 at 16:56
  • Obviously, a slightly better test would be to trigger the actual UI process that would cause the 3 parallel requests, but it'd be nice if I had a way to isolate just the DB portion and run it directly in SSMS. – Brondahl Sep 03 '20 at 17:00
  • 2
    If the reason you don't want to use multiple windows/tabs is that you want to start them at the same time, another option is to use WAITFOR. Then it will execute at the same time. e.g. WAITFOR TIME '19:08:10' – Wouter Sep 03 '20 at 17:06

1 Answers1

1

One solution to fire off 2 queries at the same time would be to use a maintenance plan. The maintenance plan could contain a job with several nodes (not connected). Each node will be executed in parallel when the job is executed.

How to triggering the maitenance plan job from a query has already been answered here.

PRO's

  • Statements are triggered at the same time.
  • Maintenance plan execution can be triggered periodically with a schedule.

CON's

  • Output of the queries is not directly visible. You would have to store the output and query the results in a new query.
Sander
  • 3,942
  • 2
  • 17
  • 22