DataRabbit 轻量的数据访问框架(15)-- IOrmAccesser的BatchInsert批量插入!

DataRabbit3.0为ORM访问器提供了批量插入的功能,其方法定义如下:
        /// <summary>
        /// BatchInsert 批量插入一组记录。忽略所有Blob字段。
        /// </summary>     
        void BatchInsert(IList<EntityType> entityList);
    当我们需要一次性向同一Table中插入大量(如千条以上)的记录时,使用BatchInsert方法可以显著的提供性能。
  我们还是以DataRabbit 轻量的数据访问框架(14)-- DataRabbit 3.0 与 Linq to sql 性能比较 一文中的例子来看看性能的变化。
  在前文的例子中,我们是这样插入1000条记录的:
        for (int i = 2000; i < 3000; i++)
        {
              accesser.Insert(new LinqTest.MyOrm.Customer(i, "Peng", 434100));
        }

  使用BatchInsert方法,我们可以这样做:
          IList<LinqTest.MyOrm.Customer> cusList = new List<LinqTest.MyOrm.Customer>();
          for (int i = 2000; i < 3000; i++)
          {
              cusList.Add(new LinqTest.MyOrm.Customer(i, "Peng", 434100));
          }
          accesser.BatchInsert(cusList);

  下面是测试的性能结果比较:


  性能提升了24倍之多,这个结果还是非常不错的,所以大批量插入Entity时,推荐使用BatchInsert方法。

  注意,BatchInsert方法在批量插入数据时将忽略所有的Blob字段。
金鳞岂是池中物,一遇风云便化龙