I have a scenario where I need to divide the insertion (insert) of records to a table in that way it wouldn't be so slow, currently I have 81K rows of records to insert, the other day using the current process took about 4-5 hrs to complete. I wanted to enhance the thread process or just divide the insert into batch say by 20's (81,000/20) and process 4k of rows everytime using for loops. Which one would be best recommendation.
This is my code currently:
iProcs = 81000/20;
Thread[] threads = new Thread[iProcs]
for (int i = 0; i < iProcs; i++)
{
//range of values to get
iStart = iRange * i;
if (i == iProcs - 1) //for last processor use the rest of the list
iEnd = packageList.Count - iStart;
else
iEnd = iRange;
var listSubset = packageList.GetRange(iStart, iEnd).ToList();
Thread myThread = new Thread(
delegate()
{
service.PostToClient(listSubset);
}
);
myThread.Start();
threads[i] = myThread;
}
// all threads should complete before we continue with main.
foreach (Thread thread in threads) { thread.Join(); }
The above code is pretty slow, I haven't test this one below if this is more effecient:
for (int i = 0; i <= iProcs; i++)
{
iStart = i * iRange;
// lets add what's been processed
iEnd = iRange;
// find out how much record is left to process
int cntleft = totalRec - iStart;
if (cntleft < iEnd)
iEnd = cntleft;
// process data save images to db
var listSubset = packageList.GetRange(iStart, iEnd).ToList();
// service.PostToDB(listSubset);
}
I'm haven't done much Thread programs so I'm not really an expert or even mediocre on it.
Any response is appreciated. Thank you.