mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-08-23 13:06:50 +08:00
Optimized parametric batch insert performance
This commit is contained in:
parent
dfdf4e82b6
commit
fbaf5411ab
@ -35,7 +35,7 @@ namespace OrmTest
|
||||
new Order() { Id = 11, Name = "order11", Price=0 },
|
||||
new Order() { Id = 12, Name = "order12" , Price=0}
|
||||
};
|
||||
|
||||
var x = db.Insertable(updateObjs).RemoveDataCache().IgnoreColumns(it => it.CreateTime).UseParameter().ExecuteCommand();
|
||||
//Ignore CreateTime
|
||||
db.Insertable(insertObj).IgnoreColumns(it => new { it.CreateTime }).ExecuteReturnIdentity();//get identity
|
||||
db.Insertable(insertObj).IgnoreColumns("CreateTime").ExecuteReturnIdentity();
|
||||
|
@ -36,6 +36,8 @@ namespace OrmTest
|
||||
new Order() { Id = 12, Name = "order12" , Price=0}
|
||||
};
|
||||
|
||||
var x = db.Insertable(updateObjs).RemoveDataCache().IgnoreColumns(it => it.CreateTime).UseParameter().ExecuteCommand();
|
||||
|
||||
//Ignore CreateTime
|
||||
db.Insertable(insertObj).IgnoreColumns(it => new { it.CreateTime }).ExecuteReturnIdentity();//get identity
|
||||
db.Insertable(insertObj).IgnoreColumns("CreateTime").ExecuteReturnIdentity();
|
||||
|
@ -10,7 +10,29 @@ namespace SqlSugar
|
||||
{
|
||||
internal IInsertable<T> Inserable { get; set; }
|
||||
internal SqlSugarProvider Context { get; set; }
|
||||
public int ExecuteCommand()
|
||||
public int ExecuteCommand()
|
||||
{
|
||||
if (this.Context.CurrentConnectionConfig.DbType.IsIn(DbType.Oracle, DbType.Dm))
|
||||
{
|
||||
return DefaultExecuteCommand();
|
||||
}
|
||||
else
|
||||
{
|
||||
return ValuesExecuteCommand();
|
||||
}
|
||||
}
|
||||
public async Task<int> ExecuteCommandAsync()
|
||||
{
|
||||
if (this.Context.CurrentConnectionConfig.DbType.IsIn(DbType.Oracle, DbType.Dm))
|
||||
{
|
||||
return await DefaultExecuteCommandAsync();
|
||||
}
|
||||
else
|
||||
{
|
||||
return await ValuesExecuteCommandAsync();
|
||||
}
|
||||
}
|
||||
public int DefaultExecuteCommand()
|
||||
{
|
||||
int result = 0;
|
||||
var inserable = Inserable as InsertableProvider<T>;
|
||||
@ -38,7 +60,7 @@ namespace SqlSugar
|
||||
return objects.Length;
|
||||
return result;
|
||||
}
|
||||
public async Task<int> ExecuteCommandAsync()
|
||||
public async Task<int> DefaultExecuteCommandAsync()
|
||||
{
|
||||
int result = 0;
|
||||
var inserable = Inserable as InsertableProvider<T>;
|
||||
@ -67,5 +89,84 @@ namespace SqlSugar
|
||||
});
|
||||
return result;
|
||||
}
|
||||
public int ValuesExecuteCommand()
|
||||
{
|
||||
|
||||
int result = 0;
|
||||
var inserable = Inserable as InsertableProvider<T>;
|
||||
var columns = inserable.InsertBuilder.DbColumnInfoList.GroupBy(it => it.DbColumnName).Select(it => it.Key).Distinct().ToList();
|
||||
var tableWithString = inserable.InsertBuilder.TableWithString;
|
||||
var removeCacheFunc = inserable.RemoveCacheFunc;
|
||||
var objects = inserable.InsertObjs;
|
||||
var identityList = inserable.EntityInfo.Columns.Where(it => it.IsIdentity).Select(it => it.PropertyName).ToArray();
|
||||
this.Context.Utilities.PageEach(objects, 100, pagelist =>
|
||||
{
|
||||
|
||||
StringBuilder batchInsetrSql;
|
||||
List<SugarParameter> allParamter=new List<SugarParameter>();
|
||||
GetInsertValues(identityList,columns, tableWithString, removeCacheFunc, pagelist, out batchInsetrSql, allParamter);
|
||||
result += this.Context.Ado.ExecuteCommand(batchInsetrSql.ToString(), allParamter);
|
||||
|
||||
});
|
||||
return result;
|
||||
|
||||
}
|
||||
public async Task<int> ValuesExecuteCommandAsync()
|
||||
{
|
||||
int result = 0;
|
||||
var inserable = Inserable as InsertableProvider<T>;
|
||||
var columns = inserable.InsertBuilder.DbColumnInfoList.GroupBy(it => it.DbColumnName).Select(it => it.Key).Distinct().ToList();
|
||||
var tableWithString = inserable.InsertBuilder.TableWithString;
|
||||
var removeCacheFunc = inserable.RemoveCacheFunc;
|
||||
var objects = inserable.InsertObjs;
|
||||
var identityList = inserable.EntityInfo.Columns.Where(it => it.IsIdentity).Select(it => it.PropertyName).ToArray();
|
||||
await this.Context.Utilities.PageEachAsync(objects, 100,async pagelist =>
|
||||
{
|
||||
|
||||
StringBuilder batchInsetrSql;
|
||||
List<SugarParameter> allParamter = new List<SugarParameter>();
|
||||
GetInsertValues(identityList, columns, tableWithString, removeCacheFunc, pagelist, out batchInsetrSql, allParamter);
|
||||
result +=await this.Context.Ado.ExecuteCommandAsync(batchInsetrSql.ToString(), allParamter);
|
||||
|
||||
});
|
||||
return result;
|
||||
}
|
||||
#region Values Helper
|
||||
private void GetInsertValues(string[] identitys, List<string> columns, string tableWithString, Action removeCacheFunc, List<T> items, out StringBuilder batchInsetrSql, List<SugarParameter> allParamter)
|
||||
{
|
||||
var itemable = this.Context.Insertable(items);
|
||||
itemable.InsertBuilder.DbColumnInfoList = itemable.InsertBuilder.DbColumnInfoList.Where(it => columns.Contains(it.DbColumnName)).ToList();
|
||||
itemable.InsertBuilder.TableWithString = tableWithString;
|
||||
(itemable as InsertableProvider<T>).RemoveCacheFunc = removeCacheFunc;
|
||||
batchInsetrSql = new StringBuilder();
|
||||
batchInsetrSql.Append("INSERT INTO " + itemable.InsertBuilder.GetTableNameString + " ");
|
||||
batchInsetrSql.Append("(");
|
||||
var groupList = itemable.InsertBuilder.DbColumnInfoList.Where(it => !identitys.Contains(it.PropertyName)).GroupBy(it => it.TableId).ToList();
|
||||
string columnsString = string.Join(",", groupList.First().Select(it => itemable.InsertBuilder.Builder.GetTranslationColumnName(it.DbColumnName)));
|
||||
batchInsetrSql.Append(columnsString);
|
||||
batchInsetrSql.Append(") VALUES");
|
||||
string insertColumns = "";
|
||||
foreach (var gitem in groupList)
|
||||
{
|
||||
batchInsetrSql.Append("(");
|
||||
insertColumns = string.Join(",", gitem.Select(it => FormatValue(it.DbColumnName, it.Value, allParamter, itemable.InsertBuilder.Builder.SqlParameterKeyWord)));
|
||||
batchInsetrSql.Append(insertColumns);
|
||||
if (groupList.Last() == gitem)
|
||||
{
|
||||
batchInsetrSql.Append(") ");
|
||||
}
|
||||
else
|
||||
{
|
||||
batchInsetrSql.Append("), ");
|
||||
}
|
||||
}
|
||||
}
|
||||
private string FormatValue(string name, object value, List<SugarParameter> allParamter, string keyword)
|
||||
{
|
||||
var result = keyword + name + allParamter.Count;
|
||||
allParamter.Add(new SugarParameter(result, value));
|
||||
return result;
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
@ -35,7 +35,7 @@ namespace OrmTest
|
||||
new Order() { Id = 11, Name = "order11", Price=0 },
|
||||
new Order() { Id = 12, Name = "order12" , Price=0}
|
||||
};
|
||||
|
||||
var x = db.Insertable(updateObjs).RemoveDataCache().IgnoreColumns(it => it.CreateTime).UseParameter().ExecuteCommand();
|
||||
//Ignore CreateTime
|
||||
db.Insertable(insertObj).IgnoreColumns(it => new { it.CreateTime }).ExecuteReturnIdentity();//get identity
|
||||
db.Insertable(insertObj).IgnoreColumns("CreateTime").ExecuteReturnIdentity();
|
||||
|
Loading…
Reference in New Issue
Block a user