1

Is there an easy way to execute queries in parallel? I have a query that has something like this:

delete from TableA where id = @id
delete from TableB where id = @id
delete from TableC where id = @id
...

and I want to do them in parallel. I can also use C#, though not sure how.

3
  • Note that if those tables have foreign key relationships between them it may not be as performant as you think. Commented Nov 10, 2011 at 1:43
  • Ya, all these tables are independent, except for a master table, which I would do last. Commented Nov 10, 2011 at 1:47
  • Is it really a perf problem and worth complicating the code with parallel processing to delete some rows by id from three tables? Commented Nov 10, 2011 at 1:51

3 Answers 3

3

First idea is to have separate threads and separate connections but I think you can manage it with multiple connections in a single thread using async callbacks:

string[] tables = new string[] { "TableA", "TableB", "TableC" ... };
var runningCommands = new List<SqlCommand>();
foreach(var table in tables)
{
  var conn = new SqlConnection(...);
  conn.Open();
  var cmd = new SqlCommand("DELETE FROM " + table + " WHERE id = @id");
  cmd.Parameters.Add(new SqlParameter("@id", id);
  cmd.BeginExecuteNonQuery(); 
  runningCommands.Add(cmd);
}
// now wait for all of them to finish executing
foreach(var cmd in runningCommands)
{
  cmd.EndExecuteNonQuery();
  cmd.Connection.Close();
}
Sign up to request clarification or add additional context in comments.

11 Comments

Parallelizing doesn't really matter - requests are still performed sequentially in the database. That's why you'd use transaction logs to restore to a particular point in time.
It may help your case for upgrade that .NET 1.1 hasn't been supported for three years: support.microsoft.com/lifecycle/search/…
@OMGPonies only DB write operations are serialized. A DELETE query is more than that (parse the query, find the right record in DB). You can benefit from that at least.
Write queries aren't serialized either. Two concurrent queries can both write to different pages in memory. Before they commit successfully they need to wait until their last LSN is written to the logfile. Restoring to a particular point in time just means restoring to those transactions that were committed at that point in time.
@ssg - The log files are always written to sequentially but that happens independent of the particular user transactions. i.e. they just carry on their work independently and write to the log cache which must be flushed at least up to the last LSN for a transaction before that transaction commits.
|
2
    List<string> list;
    list.Add("query1");
    list.Add("query2");
    list.AsParallel().ForAll(query => ExecuteQuery(query));

Comments

1

Use SSIS. Put 3 Execute sql tasks on the control flow. Add a delete statement to each task. When the package executes, they will all get executed at the same time.

You could also create a job for each statement and schedule them all to run at the same time.

Async callback would also work, but the 2 above are easier for someone with a dba skillset to implement and manage.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.