views:

30

answers:

1

I'm wondering if using by AsParallel would speed up my code in the way we use it.

I write here some very simplified pseudo code, to illustrate what I have in mind:

Let's say 3 SqlCommand under the same SqlConnection like this (pseudo code):

RunADOQueryForRequest() // returns one row
RunADOQueryForRequestAnswer() // returns about 100 rows
RunADOQueryForOtherStuff() // returns about 1000 rows (slow query)

then create 3 List of Objects (DTO's) that holds all that data:

MakeRequest()
MakeRequestAnswers()
MakeOtherStuffList()

Would it improve the performance if i would run that code as parallel (3 Parallel Tasks) ?

I know there are much if and whens. But is it worth it to write such code for Parallel?

+1  A: 

I don't think that SqlConnection class is thread-safe, so you would definitely need to create a separate instance of SqlConnection for each of the tasks. Then I think it might speed up the loading of data (but that would largely depend on the database engine).

Your comments in the first snippet suggest that the last operation takes the most of the time. If that is significantly slower than the first two operations, then there is no point it running the three in parallel (because it wouldn't be faster than the time needed to run the last task).

Parallelizing the creation of the three objects could be easier, but it depends on whether you're doing any complicated processing in the three methods. If no, then I'd guess that loading data from the database probably takes most of the time.

Tomas Petricek