C # DataSet performance best practices
1. Use ItemArray to assign values to DataRow in batches
When assigning values to all fields of DataRow, it is inefficient to assign values by column using field names. In this case, use batch field assignment whenever possible. You can use ItemArray or rows. Add:
/Ds is the data set (DataSet) object DataTable dt = ds. tables [0]; DataRow row = dt. newRow (); row. itemArray = new object [] {value1, value2 ,..., Valuen}; // ds is the data set (DataSet) object DataTable dt = ds. Tables [0]; dt. Rows. Add (value1, value2 ,..., Valuen); // avoid making a large number of consecutive Single Column assignments, as shown below: DataTable dt = ds. tables [0]; DataRow row = dt. newRow (); row [col1] = value1; row [col2] = value2 ;... Row [coln] = valuen;
2. reasonably use the parallel computing of DataTable
DataTable's built-in parallel computing can make full use of each CPU of the computer to optimize the efficiency.
IEnumerable
FindRows () // find all the entries whose quantity is less than 0 {DataTable dt = ItemDataTable ;...... Return dt. Select ("Quantity <0"); // parallel computing not used} IEnumerable
FindRows () // find all the entries whose quantity is less than 0 {DataTable dt = ItemDataTable ;...... Int index = dt. columns. indexOf (Quantity); return dt. asEnumerable (). asParallel (). where (dr => (decimal) dr [index] <0); // use parallel computing :}
According to the experiment, parallel computing is better than Select and loop filtering when selecting the row of the DataTable; The performance is similar when the row traversal is performed.
3. Use ImportRow to merge data tables with the same structure
The Merge method can be used to Merge DataTable conveniently, but Merge is very inefficient in code. The example is as follows:
DataTable[] srcTables = ... ;foreach(DataTable src in srcTables ){dest.Merge( src ) ;}
ImportRow can also be used to Merge able, which has a much higher performance than Merge. The sample code is as follows:
DataTable[] srcTables = ... ;foreach(DataTable src in srcTables ){ foreach(DataRow row in src.Rows) { dest.ImportRow( row ) ; }}