Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running Different Stored Procedures in Parallel in EF6

I have many different stored procedures that I want to fire off in Parallel to improve the performance of my MVC web application. These stored procedures gather information from multiple sources and return different data types. For the sake of simplicity, let's say I only had two, returning different complex data types:

  • ups_Proc1 returns a List of usp_Proc1
  • usp_Proc2 returns a List of usp_Proc2

and so on....

I could do one of these numbers, but it would fire them off in a series:

List<usp_Task1> taskOneResult = await db.usp_Task1(parms).AsQueryable()
                                                         .ToListAsync();
List<usp_Task2> taskTwoResult = await db.usp_Task2(parms).AsQueryable()
                                                         .ToListAsync();

The solutions I've seen use await Task.WhenAll() and you pass in an array of tasks, but mine are tasks with different return types. So how does one fire multiple stored procedures in parallel when dealing with different complex return types?

UPDATE- 5/20/2015

Finally got a working solution that I believe is handling all the tasks in parallel. I had to use Task.Factory.StartNew() command to create each function as a task to be passed into Task.WaitAll().

I rapped all this into a new class that contained a List<> for each stored procedure return type I'm working with, allowing me to handle all my business logic there. The code in the constructor looked a little like this:

var task1 = Task.Factory.StartNew(() => CallStoredProc1(parms));
var task2 = Task.Factory.StartNew(() => CallStoredProc2(parms));

var taskList = new List<Task> { task1, task2 };

Task.WaitAll(taskList.ToArray());

CallStoredProc1() and CallStoredProc2() are private void methods where I do my stored procedure calls and handle data conversion.

Any feedback is greatly appreciated!

like image 886
Scarlety Avatar asked May 18 '15 15:05

Scarlety


1 Answers

Figured I would repost my update as a solution.

Finally got a working solution that I believe is handling all the tasks in parallel. I had to use Task.Factory.StartNew() command to create each function as a task to be passed into Task.WaitAll().

I rapped all this into a new class that contained a List<> for each stored procedure return type I'm working with, allowing me to handle all my business logic there. The code in the constructor looked a little like this:

var task1 = Task.Factory.StartNew(() => CallStoredProc1(parms));
var task2 = Task.Factory.StartNew(() => CallStoredProc2(parms));

var taskList = new List<Task> { task1, task2 };

Task.WaitAll(taskList.ToArray());

CallStoredProc1() and CallStoredProc2() are private void methods where I do my stored procedure calls and handle data conversion.

Any feedback is greatly appreciated!

like image 98
Scarlety Avatar answered Sep 20 '22 14:09

Scarlety