FreeSql (xiv) batch update data

FreeSql supports rich data update method, support single or batch updates, you can also return the updated value recorded in a specific database to perform.

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

IFreeSql fsql = new FreeSql.FreeSqlBuilder()
    .UseConnectionString(FreeSql.DataType.MySql, connstr)
    .UseAutoSyncStructure(true) //自动同步实体结构到数据库
    .Build();

[Table(Name = "tb_topic")]
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; }
}

Batch Update

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.Update<Topic>().SetSource(items).ExecuteAffrows();
//UPDATE `tb_topic` SET `Clicks` = CASE `Id` WHEN 1 THEN ?p_0 WHEN 2 THEN ?p_1 WHEN 3 THEN ?p_2 WHEN 4 THEN ?p_3 WHEN 5 THEN ?p_4 WHEN 6 THEN ?p_5 WHEN 7 THEN ?p_6 WHEN 8 THEN ?p_7 WHEN 9 THEN ?p_8 WHEN 10 THEN ?p_9 END, `Title` = CASE `Id` WHEN 1 THEN ?p_10 WHEN 2 THEN ?p_11 WHEN 3 THEN ?p_12 WHEN 4 THEN ?p_13 WHEN 5 THEN ?p_14 WHEN 6 THEN ?p_15 WHEN 7 THEN ?p_16 WHEN 8 THEN ?p_17 WHEN 9 THEN ?p_18 WHEN 10 THEN ?p_19 END, `CreateTime` = CASE `Id` WHEN 1 THEN ?p_20 WHEN 2 THEN ?p_21 WHEN 3 THEN ?p_22 WHEN 4 THEN ?p_23 WHEN 5 THEN ?p_24 WHEN 6 THEN ?p_25 WHEN 7 THEN ?p_26 WHEN 8 THEN ?p_27 WHEN 9 THEN ?p_28 WHEN 10 THEN ?p_29 END WHERE (`Id` IN (1,2,3,4,5,6,7,8,9,10))

Batch updates scenes, first check 20 records, according to the local rules of the very complex set of values ​​changed after

Traditional practice is to save 20 cycles, with the case when just once on the line

Batch updates, ignore some columns

fsql.Update<Topic>().SetSource(items).IgnoreColumns(a => new { a.Clicks, a.CreateTime }).ExecuteAffrows();
//UPDATE `tb_topic` SET `Title` = CASE `Id` WHEN 1 THEN ?p_0 WHEN 2 THEN ?p_1 WHEN 3 THEN ?p_2 WHEN 4 THEN ?p_3 WHEN 5 THEN ?p_4 WHEN 6 THEN ?p_5 WHEN 7 THEN ?p_6 WHEN 8 THEN ?p_7 WHEN 9 THEN ?p_8 WHEN 10 THEN ?p_9 END WHERE (`Id` IN (1,2,3,4,5,6,7,8,9,10))

Batch Updates the designated column

fsql.Update<Topic>().SetSource(items).Set(a => a.CreateTime, DateTime.Now).ExecuteAffrows();
//UPDATE `tb_topic` SET `CreateTime` = ?p_0 WHERE (`Id` IN (1,2,3,4,5,6,7,8,9,10))

After the specified column update, save the batch will fail

Internally

When large quantities of data update, the internal logic segmentation is performed batchwise. Segmentation rules are as follows:

Quantity The amount of parameters
MySql 5000 3000
PostgreSQL 5000 3000
SqlServer 1000 2100
Oracle 500 999
Sqlite 5000 999

Data: dividing the size of each batch, such as bulk data insertion 10000, execution will when mysql divided into two batches.
Parameters amount: is an amount the size of the batch split parameters, such as bulk data insertion 10000, each row of five parameters required when performing splits each batch mysql 3000/5.

After performing segmentation, when the transaction is not provided outside the internal transaction from the open to achieve insertion integrity.

FreeSql the adapted parametric each data type, and non-parametric use. Bulk Insert the proposed closure of the parametric function, use .NonoParameter () for execution.

API

method return value parameter description
SetSource <this> T1 | IEnumerable Update data, set the updated entity
IgnoreColumns <this> Lambda Ignore column
Set <this> Lambda, value Setting a new value column, Set (a => a.Name, "newvalue")
Set <this> Lambda Setting a new value of the column on the basis of the increase, Set (a => a.Clicks + 1), corresponding to clicks = clicks + 1;
SetRaw <this> string, parms Set value, a custom SQL syntax, SetRaw ( "title =? Title", new {title = "newtitle"})
Where <this> Lambda Expression conditions, only the physical infrastructure to support members (not contain navigation objects)
Where <this> string, parms Sql syntax native conditions, Where ( "id =? Id", new {id = 1})
Where <this> T1 | IEnumerable Incoming entity or set as a condition to its primary key
WhereExists <this> ISelect The existence of sub-query
WithTransaction <this> DbTransaction Set transaction object
ToSql string Returns the SQL statement to be executed
ExecuteAffrows long The number of rows to execute SQL statements, the impact of return
ExecuteUpdated List<T1> Execute SQL statements, returns the updated record

Guess you like

Origin www.cnblogs.com/FreeSql/p/11531335.html