I had a scenario where I wanted to query several database tables at once and process the data as fast as possible using a single database connection.
After some though I decided to look at MARS ( multiple active result set) which was available from SQL2005 and up. This gives the option to open multiple datareaders under the one SQL database connection and allow the data to be read in an interleaved fashion. Although not true parallel reading it does have the advantage of having only one database connection open.
Also I had been reading about the new parallel framework that is available in .Net 4.0. Because I have several datareaders open, I could process each reader in parallel (using multi-core processors) and take advantage of the yeild points. This is because statements which return rows to the client, are allowed to interleave execution before completion while rows are being sent to the client.
Below is the sample prototype code to test my theory.
I used the Parallel.Invoke method because I need to wait until I received all the data back form all the threads.
Below is the SQL Profiler trace after executing the code in quick succession. On the first query set, the ‘Address’ batch yielded until the ‘Contact’ batch completed. However on the highlighted second query the ‘Contact’ batch waited until the ‘Address’ batch completed.