-2

I've been reading up on executing tasks in parallel. I have multiple database row updates (each one independent) that need to occur. Instead of updating one row at a time, I want to update them all at once. Note in below code that dbConnection.ExecuteAsync returns a Task<int> (i.e. # of rows updated/deleted).

Below is my current approach:

var deleteTasks = new List<Task<int>>();

foreach (var item in queries)
{
   deleteTasks.Add(dbConnection.ExecuteAsync(item.RawSql, item.Parameters, transaction: transaction));
}

//ensure all queries are complete
await Task.WhenAll(deleteTasks);

Now above approach works, but I've also seen below two approaches:

var deleteTasksSelect1 = queries.Select(query => (dbConnection.ExecuteAsync(query.RawSql, query.Parameters, transaction: transaction)));

var deleteTasksSelect2 = queries.Select(async query => await (dbConnection.ExecuteAsync(query.RawSql, query.Parameters, transaction: transaction)));

Both of them return IEnumerable<Task<int>>. I'm trying to understand what the difference is between my approach above and the LINQ projections. One of them builds up an async Func while the other does not but both return the same result. What is the difference between these and what is the correct approach?


Note that for getting my results (i.e. the # of rows affected, I do below after my call to Task.WhenAll()):

var successCount = deleteTasks.Sum(x => x.Result)
Amit Joshi
  • 15,448
  • 21
  • 77
  • 141
Help123
  • 1,511
  • 2
  • 28
  • 46

1 Answers1

0

I've been reading up on executing tasks in parallel. I have multiple database row updates (each one independent) that need to occur. Instead of updating one row at a time, I want to update them all at once. Note in below code that dbConnection.ExecuteAsync returns a Task (i.e. # of rows updated/deleted).

That is going to do the opposite of helping. If you got a bunch of DB operations, do not send them against the DB piecemeal. The amount of data you have to transfer is fixed, so all you do is add a ton of Network connection and Transaction overhead to each and every one of them. Those two are the things you need to minimize. That lesson comes right after "do not retreiving more data then you need, to do filtering in the UI".

The table lockouts will propably not allow a paralell execution anyway. It is nice that you asume they are independant (wich might be wrong), the DB can not be so sure of it. If you send a bunch of DML statements, it will write lock the table (wich very likely also prevents any selects from running on them) for each and every one of them. DB's are all about turning concurrent requests into a nice sequential operation.

If you want to learn threadless multitasking, you need something that interacts with many servers - not just one. Stuff like a "is this Website online" test tools, maybe?

Christopher
  • 9,634
  • 2
  • 17
  • 31