简体   繁体   中英

For loops vs thread Pool in c#

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.

  1. Fist of all use SqlBulkCopy(for sql server) or other BulkCopy (depends on DB vendor) if possible
  2. If you need insert data asap, so perform some benchmark with multiple inserting threads on real hardware. By this data you can optimize number of inserting threads for best time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM