Loading

FreeSql (六)批量插入数据

var connectionString = "Data Source=127.0.0.1;Port=3306;User ID=root;Password=root;" + 
    "Initial Catalog=cccddd;Charset=utf8;SslMode=none;Max pool size=10";

static IFreeSql fsql = new FreeSql.FreeSqlBuilder()
    .UseConnectionString(FreeSql.DataType.MySql, connectionString)
    .UseAutoSyncStructure(true) //自动同步实体结构到数据库
    .Build(); //请务必定义成 Singleton 单例模式

class Topic {
    [Column(IsIdentity = true, IsPrimary = true)]
    public int Id { get; set; }
    public int Clicks { get; set; }
    public string Title { get; set; }
    public DateTime CreateTime { get; set; }
}

批量插入

var items = new List<Topic>();
for (var a = 0; a < 10; a++) items.Add(new Topic { Id = a + 1, Title = $"newtitle{a}", Clicks = a * 100 });

fsql.Insert(items).ExecuteAffrows();

执行SQL如下:

INSERT INTO `tb_topic`(`Clicks`, `Title`, `CreateTime`) 
VALUES(@Clicks0, @Title0, @CreateTime0), 
(@Clicks1, @Title1, @CreateTime1), 
(@Clicks2, @Title2, @CreateTime2), 
(@Clicks3, @Title3, @CreateTime3), 
(@Clicks4, @Title4, @CreateTime4), 
(@Clicks5, @Title5, @CreateTime5), 
(@Clicks6, @Title6, @CreateTime6), 
(@Clicks7, @Title7, @CreateTime7), 
(@Clicks8, @Title8, @CreateTime8), 
(@Clicks9, @Title9, @CreateTime9)

内部设计

当插入大批量数据时,内部采用分割分批执行的逻辑进行。分割规则如下:

数量 参数量
MySql 5000 3000
PostgreSQL 5000 3000
SqlServer 1000 2100
Oracle 500 999
Sqlite 5000 999

数据:为每批分割的大小,如批量插入 10000 条数据,在 mysql 执行时会分割为两批。
参数量:为每批分割的参数量大小,如批量插入 10000 条数据,每行需要使用 5 个参数化,在 mysql 执行时会分割为每批 3000 / 5。

分割执行后,当外部未提供事务时,内部自开事务,实现插入完整性。

FreeSql 适配了每一种数据类型参数化,和不参数化的使用。批量插入建议关闭参数化功能,使用 .NonoParameter() 进行执行(有关 NoneParameter 在后续文章介绍)。

性能参考

导入表数据

int affrows = fsql.Select<Topic>()
  .Limit(10)
  .InsertInto(null, a => new Topic2
  {
    Title = a.Title
  });
INSERT INTO `Topic2`(`Title`, `Clicks`, `CreateTime`)
SELECT a.`Title`, 0, '0001-01-01 00:00:00' 
FROM `Topic` a 
limit 10

注意:因为 Clicks、CreateTime 没有被选择,所以使用目标实体属性 [Column(InsertValueSql = xx)] 设置的值,或者使用目标实体属性的 c# 默认值。

API

方法 返回值 参数 描述
AppendData <this> T1 | IEnumerable 追加准备插入的实体
ToSql string 返回即将执行的SQL语句
ExecuteAffrows long 执行SQL语句,返回影响的行数
ExecuteIdentity long 执行SQL语句,返回自增值
ExecuteInserted List<T1> 执行SQL语句,返回插入后的记录
ExecuteSqlBulkCopy void SqlServer 特有的功能,执行 SqlBulkCopy 批量插入的封装
ExecutePgCopy void PostgreSQL 特有的功能,执行 Copy 批量导入数据

系列文章导航

posted @ 2019-09-17 06:30  FreeSql  阅读(10422)  评论(7编辑  收藏  举报