mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-06-18 20:08:15 +08:00
- 增加 IInsertOrUpdate 高性能方法 ExecutePgCopy;
This commit is contained in:
@ -37,6 +37,51 @@ public static partial class FreeSqlPostgreSQLGlobalExtensions
|
||||
|
||||
#region ExecutePgCopy
|
||||
/// <summary>
|
||||
/// 批量插入或更新(操作的字段数量超过 2000 时收益大)<para></para>
|
||||
/// 实现原理:使用 PgCopy 插入临时表,再执行 INSERT INTO t1 select * from #temp ON CONFLICT(""id"") DO UPDATE SET ...
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="that"></param>
|
||||
/// <returns></returns>
|
||||
public static int ExecutePgCopy<T>(this IInsertOrUpdate<T> that) where T : class
|
||||
{
|
||||
var upsert = that as InsertOrUpdateProvider<T>;
|
||||
if (upsert._source.Any() != true || upsert._tempPrimarys.Any() == false) return 0;
|
||||
var state = ExecutePgCopyState(upsert);
|
||||
return UpdateProvider.ExecuteBulkUpsert(upsert, state, insert => insert.ExecutePgCopy());
|
||||
}
|
||||
static NativeTuple<string, string, string, string, string[]> ExecutePgCopyState<T>(InsertOrUpdateProvider<T> upsert) where T : class
|
||||
{
|
||||
if (upsert._source.Any() != true) return null;
|
||||
var _table = upsert._table;
|
||||
var _commonUtils = upsert._commonUtils;
|
||||
var updateTableName = upsert._tableRule?.Invoke(_table.DbName) ?? _table.DbName;
|
||||
var tempTableName = $"Temp_{Guid.NewGuid().ToString("N")}";
|
||||
if (upsert._orm.CodeFirst.IsSyncStructureToLower) tempTableName = tempTableName.ToLower();
|
||||
if (upsert._orm.CodeFirst.IsSyncStructureToUpper) tempTableName = tempTableName.ToUpper();
|
||||
if (upsert._connection == null && upsert._orm.Ado.TransactionCurrentThread != null)
|
||||
upsert.WithTransaction(upsert._orm.Ado.TransactionCurrentThread);
|
||||
var sb = new StringBuilder().Append("CREATE TEMP TABLE ").Append(_commonUtils.QuoteSqlName(tempTableName)).Append(" ( ");
|
||||
foreach (var col in _table.Columns.Values)
|
||||
{
|
||||
sb.Append(" \r\n ").Append(_commonUtils.QuoteSqlName(col.Attribute.Name)).Append(" ").Append(col.Attribute.DbType.Replace("NOT NULL", ""));
|
||||
sb.Append(",");
|
||||
}
|
||||
var sql1 = sb.Remove(sb.Length - 1, 1).Append("\r\n) WITH (OIDS=FALSE);").ToString();
|
||||
sb.Clear();
|
||||
try
|
||||
{
|
||||
upsert._sourceSql = $"select * from {tempTableName}";
|
||||
var sql2 = upsert.ToSql();
|
||||
var sql3 = $"DROP TABLE {_commonUtils.QuoteSqlName(tempTableName)}";
|
||||
return NativeTuple.Create(sql1, sql2, sql3, tempTableName, _table.Columns.Values.Select(a => a.Attribute.Name).ToArray());
|
||||
}
|
||||
finally
|
||||
{
|
||||
upsert._sourceSql = null;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 批量更新(更新字段数量超过 2000 时收益大)<para></para>
|
||||
/// 实现原理:使用 PgCopy 插入临时表,再使用 UPDATE INNER JOIN 联表更新
|
||||
/// </summary>
|
||||
@ -173,6 +218,13 @@ public static partial class FreeSqlPostgreSQLGlobalExtensions
|
||||
|
||||
#if net45
|
||||
#else
|
||||
public static Task<int> ExecutePgCopyAsync<T>(this IInsertOrUpdate<T> that, CancellationToken cancellationToken = default) where T : class
|
||||
{
|
||||
var upsert = that as UpdateProvider<T>;
|
||||
if (upsert._source.Any() != true || upsert._tempPrimarys.Any() == false) return Task.FromResult(0);
|
||||
var state = ExecutePgCopyState(upsert);
|
||||
return UpdateProvider.ExecuteBulkUpdateAsync(upsert, state, insert => insert.ExecutePgCopyAsync(cancellationToken));
|
||||
}
|
||||
public static Task<int> ExecutePgCopyAsync<T>(this IUpdate<T> that, CancellationToken cancellationToken = default) where T : class
|
||||
{
|
||||
var update = that as UpdateProvider<T>;
|
||||
|
Reference in New Issue
Block a user