最近的一项工作是关于性能提升方面的。要做的第一个事情是要把很多同类型的DataTable合并到一起,查了很多关于DataTable的相关函数以后,我决定用Merge函数来合并这些DataTable。
      DataTable[] srcTables = ... ;
      foreach( DataTable src in srcTables )
      {
         dest.Merge( src ) ;
      }
      但是测试的结果让我很是失望,性能不是一般的不好。经过调查发现性能的瓶颈在Merge函数这里。后来经过测试,发现如果用下面的代码:
      DataTable[] srcTables = ... ;
      foreach( DataTable src in srcTables )
      {
         foreach( DataRow row in src.Rows)
         {
            dest.ImportRow( row ) ;      
         }

      }
   
      结果让人惊奇的是,下面的代面的速度是上面的代码速度的100倍!

      还做了一个事情,就是对DataTable进行filter的时候 ,我的一个同事和我说了以下的代码:
      DataView dv = dt.DefaultView ;
      dv.RowFilter = filter ;
      DataTable result = dv.ToTable() ;
      上面的代码是能工作的,但是它的性能一点都不好,后来我把上面的代码改成了:
      DataRow[] rows = dv.Select( filter ) ;
      foreach( DataRow row in rows )
      {
         result.ImportRow(row) ;
      }

      也有数十倍的性能提高。

      没想到有这么多的朋友看这个文章,我看了一下Table的Merge函数,下面是关键函数:


private void MergeTable(DataTable src, DataTable dst) {                                                   
    int count = src.Rows.Count;                                                                       
    bool flag = dst.Rows.Count == 0;                                                                  
    if (0 < count) {                                                                                  
        Index ndx = null;                                                                         
        DataKey srcKey = new DataKey();                                                           
        dst.SuspendIndexEvents();                                                                 
        try {                                                                                     
            if (!flag && (dst.primaryKey != null)) {                                          
                srcKey = this.GetSrcKey(src, dst);                                        
                if (srcKey.HasValue) {                                                    
                    ndx = dst.primaryKey.Key.GetSortIndex(DataViewRowState.OriginalRows
            | DataViewRowState.Added);                                                        
                }                                                                         
            }                                                                                 
            for (int i = 0; i < count; i++) {                                                 
                DataRow row2 = src.Rows[i];                                               
                DataRow targetRow = null;                                                 
                if (ndx != null) {                                                        
                    targetRow = dst.FindMergeTarget(row2, srcKey, ndx);               
                }                                                                         
                dst.MergeRow(row2, targetRow, this.preserveChanges, ndx);                 
            }                                                                                 
        } finally {                                                                               
            dst.RestoreIndexEvents(true);                                                     
        }                                                                                         
    }                                                                                                 
    this.MergeExtendedProperties(src.ExtendedProperties, dst.ExtendedProperties);                     
}                                                                                                         


      从中我们可以看到Merge做了什么,Merge Table的时候,对每一行,都要有一个对Index进行操作的过程,查看现在要进行insert的row是不是存在重复等等操作。而对我们只想进行append操作的时候,显然是多余的了。