Skip to content

Commit c78fd13

Browse files
committed
空表导入时,默认就是批量插入,拥有很好的性能;非空表时,合并会比较慢(一般是批量插入的10倍以上耗时)。
业务分区的数据(例如按天ds分区),如果导入某个分区数据,开发者可以重载,查询该分区行数赋值给TotalCount,促使父类实现在空数据时执行批量插入。
1 parent 5bb7c95 commit c78fd13

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

NewLife.Cube/Common/EntityController2.cs

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -296,18 +296,32 @@ protected virtual Int32 OnMerge(IEntityFactory factory, IList<IEntity> list, Imp
296296

297297
/// <summary>导入数据默认保存</summary>
298298
/// <remarks>
299-
/// 导入的新数据合并到旧数据,已有更新,没有则插入
300-
/// 主要按主键来查找判断是否已存在。
299+
/// 默认导入模式Auto,如果TotalCount是0则直接插入,否则按Merge合并处理
300+
/// 默认合并逻辑:导入的新数据合并到旧数据,已有更新,没有则插入。主要按主键来查找判断是否已存在。
301301
/// 该方案并不全面,需要使用者自己重载来实现精细化的合并逻辑。
302+
///
303+
/// 因此,空表导入时,默认就是批量插入,拥有很好的性能;非空表时,合并会比较慢(一般是批量插入的10倍以上耗时)。
304+
/// 业务分区的数据(例如按天ds分区),如果导入某个分区数据,开发者可以重载,查询该分区行数赋值给TotalCount,促使父类实现在空数据时执行批量插入。
302305
/// </remarks>
303-
/// <param name="factory"></param>
304-
/// <param name="list"></param>
305-
/// <param name="context"></param>
306+
/// <param name="factory">实体工厂</param>
307+
/// <param name="list">新数据列表</param>
308+
/// <param name="context">导入上下文(含表头与字段)</param>
306309
/// <returns></returns>
307310
protected virtual Int32 OnImport(IEntityFactory factory, IList<IEntity> list, ImportContext context)
308311
{
309312
if (list.Count == 0) return 0;
310313

314+
// 总行数
315+
var totalRows = 0L;
316+
if (context.TotalCount != null)
317+
totalRows = context.TotalCount.Value;
318+
else
319+
{
320+
totalRows = factory.Session.Count;
321+
if (totalRows < 10000) totalRows = (Int32)factory.FindCount();
322+
context.TotalCount = totalRows;
323+
}
324+
311325
// 如果是当前实体类型,直接转换为强类型列表,提高性能
312326
if (factory == Factory && list[0] is TEntity)
313327
{
@@ -319,14 +333,10 @@ protected virtual Int32 OnImport(IEntityFactory factory, IList<IEntity> list, Im
319333
ImportMode.Replace => typed.BatchReplace(),
320334
ImportMode.Upsert => typed.Upsert(),
321335
ImportMode.Merge => OnMerge(factory, list, context),
322-
_ => OnMerge(factory, list, context),
336+
_ => totalRows == 0 ? typed.Insert() : OnMerge(factory, list, context),
323337
};
324338
}
325339

326-
// 总行数
327-
var totalRows = factory.Session.Count;
328-
if (totalRows < 10000) totalRows = (Int32)factory.FindCount();
329-
330340
// 根据导入模式进行处理
331341
switch (context.Mode)
332342
{
@@ -343,6 +353,7 @@ protected virtual Int32 OnImport(IEntityFactory factory, IList<IEntity> list, Im
343353
// 冲突时更新
344354
return list.Upsert();
345355
case ImportMode.Merge:
356+
return OnMerge(factory, list, context);
346357
case ImportMode.Auto:
347358
default:
348359
{

NewLife.Cube/Models/ImportContext.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ public class ImportContext : IExtend
3333
/// <summary>字段列表</summary>
3434
public FieldItem[] Fields { get; set; }
3535

36+
/// <summary>数据表已有总行数。用于批判导入时使用哪一种策略</summary>
37+
public Int64? TotalCount { get; set; }
38+
3639
/// <summary>扩展字段</summary>
3740
[XmlIgnore, ScriptIgnore]
3841
[EditorBrowsable(EditorBrowsableState.Never)]

0 commit comments

Comments
 (0)