In an application I am working on, it needs to read data from about 5 to 10 tables in the same SQL Server database, load them into multiple List<> and process the data afterwards. The number of rows returned from each table varies, 1 row for some and about 1,000 rows for some others.
The application is written in C# 4.0, so I was thinking of using Task Parallel Library and start multiple tasks to retrieve data from these tables simultaneously. I thought it would be faster than calling a single query that returns the data from these tables in a sequence, but don't know for sure. So my questions are:
- Considering the overheads such as opening multiple connections to the database, would it really be faster to retrieve data using multiple tasks?
- If the answer is Yes in some cases, what are the cases where the answer is No?
- If the answer is No, is there any alternative approach?
- Any other things to consider when using multiple tasks to retrieve data?
To narrow down the scope, here are some assumptions you can make:
- Data processing is not part of the question. Just the data retrieval alone.
- Some of these tables are related with FKs. Like Parent <-- Child <-- Grandchild
- These tables only contain varchar and numeric columns, and the row size is less than 400 bytes.
- Plain ADO.NET (e.g. SqlConnection/SqlCommand/SqlDataReader) is used to retrieve the data from each table
- Each task will call a self-contained method that reads the data from a SqlDataReader and returns a List of objects.
- The database server is powerful enough to handle all concurrent connections and queries.
- The client computer is powerful enough to handle the data operation if using single query to retrieve all data in a sequence. And it is capable of doing multi-threading.
- Network is fast enough for transmitting data if using single query
Thank you in advance for your time and input!