I have two DataTables:
dt
: is populated from a CSV file with over 1.7 million rowsdataStructure.Tables["AccountData"]
: is populated from a database query also roughly a million rows
I use the following code to iterate through and compare the data from each set of rows. The code takes over 48 hours to complete. I have changed the properties of the application to x64 to allow it to use more Process Memory. It now uses roughly 2.5GB.
My question is, is there a more efficient way of doing this that would decrease run time?
//set is_legal column value for each row
foreach (DataRow row in dt.Rows)
{
var acctNum = row[0].ToString().Replace("\"", "");
foreach (DataRow queryRow in dataStructure.Tables["AccountData"].Rows)
{
var queryAcctNum = queryRow[0].ToString();
if (acctNum.Equals(queryAcctNum))
{
row[12] = "Y";
Console.WriteLine("Yes count: " + cnt);
}
else
{
row[12] = "N";
}
}
cnt++;
};
How dataStructure.Tables["AccountData"]
is being populated:
//Read each row from the table and output the results into the data set
while (readFile.Read())
{
//Create a row to hold data
DataRow datarow = dataStructure.Tables["AccountData"].NewRow();
datarow["AccountNumber"] = readFile.GetString(0).Trim();
datarow["LegalStatus"] = readFile.GetString(1);
//add the row to the data table AccountData
dataStructure.Tables["AccountData"].Rows.Add(datarow);
}
Console.WriteLine
in your real code? You'll have to run a profiler to find what is taking the time but that's where I'd put my money. \$\endgroup\$break;
ing after finding a match so all records will likely haverow[12] == "N"
. \$\endgroup\$