I have a large DataTable that contains Users details. I need to complete user's details into this table from several tables in DB. I run though each row in the table and make several calls to different tables in the database, using ADO.NET objects and methods, process and reorganize the results and them to the main table. It's works fine, but to slow... My idea was to split the large table into few small tables and run the CompleteAddressDetails method in a few threads simultaneously and in the end to merge small tables into one result table. I have implemented this idea using Task object of TPL. There is a code below. It works fine, but without any improvement of execution time. Several questions: 1. Why there no any improvement of execution time? 2. What I have to do in order to improve it?
Thank you for any advice!
resultTable1 = data.Clone();
resultTable2 = data.Clone();
resultTable3 = data.Clone();
resultTable4 = data.Clone();
resultTable5 = data.Clone();
DataTable[] tables = new DataTable[] { resultTable1, resultTable2, resultTable3, resultTable4, resultTable5 };
for (int i = 0; i < data.Rows.Count; i += 5)
{
for (int j = 0; j < 5; j++)
{
if (data.Rows.Count > i + j)
{
tables[j].Rows.Add(data.Rows[i + j].ItemArray);
}
}
}
Task[] taskArray = {Task.Factory.StartNew(() =>CompleteAddressDetails(resultTable1)),
Task.Factory.StartNew(() =>CompleteAddressDetails(resultTable2)),
Task.Factory.StartNew(() =>CompleteAddressDetails(resultTable3)),
Task.Factory.StartNew(() =>CompleteAddressDetails(resultTable4)),
Task.Factory.StartNew(() =>CompleteAddressDetails(resultTable5))};
Task.WaitAll(taskArray);
Task.Factory.StartNewis not the best way forward here. It would be better to rewrite theCompleteAddressDetailsmethod to be properly asynchronous (async/await). Given the level of detail you provide, it's not possible to say where you're bottlenecking. How fast do the queries execute when you run them directly into the database? Are you sure you've got everything covered with respect to indexes on the tables that your query would benefit from?