Caution When Using SetParentRow of a DataSet

I had a serious performance killer when using SetParentRow in the creation of a DataSet in ADO.NET.
 
After some trial and error, I found out it is very important to call SetParentRow first and add the childRow to the child table later.
 
In the first case, the performance of SetParentRow is linear, in the second case, it is exponential when adding more rows. Check out the C# examples and results.
 
Version 1 = fast
  1. for (j = 0; j < (number of parentROws); j++) {  
  2.       parentRow = parent.NewRow();  
  3.       parentRow["parentID"] = j;  
  4.       parentRow["Name"] = "Parent" + j.ToString();  
  5.       parent.Rows.Add(parentRow);  
  6.       for (int i = 0; i < 10; i++) {  
  7.             childRow = childs.NewRow();  
  8.             childRow["childName"] = "ABCDFEGHIJKL"  
  9.             childRow["childId"] = (i + 1);  
  10.             childRow.SetParentRow(parentRow); // 1: set relation  
  11.             childs.Rows.Add(childRow); // 2: add row  
  12.       }  
  13. }  
Version 2 = slow
  1. for (j = 0; j < (number of parentROws); j++) {  
  2.       parentRow = parent.NewRow();  
  3.       parentRow["parentID"] = j;  
  4.       parentRow["Name"] = "Parent" + j.ToString();  
  5.       parent.Rows.Add(parentRow);  
  6.       for (int i = 0; i < 10; i++) {  
  7.             childRow = childs.NewRow();  
  8.             childRow["childName"] = "ABCDFEGHIJKL"  
  9.             childRow["childId"] = (i + 1);  
  10.             childs.Rows.Add(childRow); // 1: add row  
  11.             childRow.SetParentRow(parentRow); // 2: set relation  
  12.       }  
  13. }  
processing time in seconds:
#parentrows v1 v2
1000 0,64 1,20
2000 1,20 4,21
3000 1,87 16,56
4000 2,60 53,86
5000 3,40 116,23
6000 4,28 211,97
7000 5,67 347,43
8000 6,37 527,09
9000 6,92 761,68
10000 7,14 1057,74


Similar Articles