I have SQL database table and suppose it contains 3000 records, now i want to process all these records with 3 threads. 1000 records process in 1st thread like that. How it is possible in c# multi threading?
Asked
Active
Viewed 566 times
-1
-
The SQL Server is multi-threaded so creating additional threads in only will speed up process if the transfer of the data is limiting the speed. Best way of speeding up app is to use Entity which is faster than SQL Client Class. – jdweng May 03 '20 at 10:38
-
@jdweng *use Entity which is faster than SQL Client Class* any sources? how Entity could be faster if is builded on top SQL Client Class? – Selvin May 03 '20 at 10:47
-
Entity is not build on SQL Client. Entity is build on db context which is a different driver. – jdweng May 03 '20 at 10:59
-
What kind of processing is that? Is it [CPU-bound or I/O bound](https://stackoverflow.com/questions/868568/what-do-the-terms-cpu-bound-and-i-o-bound-mean)? – Theodor Zoulias May 03 '20 at 12:02
-
Entity is build on top of Microsoft.Data.SqlClient ... obviously using Microsoft.Data.SqlClient directly will be faster – Selvin May 03 '20 at 12:28
2 Answers
0
The most straight forward approach would be to retrieve all rows by filling a DataTable
, then splitting the DataTable into smaller subsets of data as other DataTable and passing those off to each thread.
Take a look at the answer in this thread on how to split a DataTable into multiple DataTables.

jscarle
- 1,045
- 9
- 17
0
You can create tasks and in each task you can process a batch of rows.
var tasks = new List<Task>();
for (var i = 0; i < 3; i++)
{
tasks.Add(new Task(() => doSomething(context.Subscriptors.Skip(i).Take(1000))));
}
Task.WhenAll(tasks);
//continue execution

Theodor Zoulias
- 34,835
- 7
- 69
- 104

Javier Pazos
- 119
- 2
- 6
-
Thank you so much for this information. But still i'm confuse that how may i use 3 thread while at a time in database added 3000 records and i want use 3 threads for it. and also want to use sql transaction in thread. please can you share more information for this. – Nirav May 06 '20 at 14:24