From 279aeb5a5b8d0f09eeb36abf112950f1718f89a9 Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Tue, 12 Dec 2023 20:34:24 +0800 Subject: [PATCH] Performance optimization --- .../InsertableProvider/ParameterInsertable.cs | 38 +++++++++++++++---- 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/Src/Asp.NetCore2/SqlSugar/Abstract/InsertableProvider/ParameterInsertable.cs b/Src/Asp.NetCore2/SqlSugar/Abstract/InsertableProvider/ParameterInsertable.cs index d08ee8922..6b8c92a45 100644 --- a/Src/Asp.NetCore2/SqlSugar/Abstract/InsertableProvider/ParameterInsertable.cs +++ b/Src/Asp.NetCore2/SqlSugar/Abstract/InsertableProvider/ParameterInsertable.cs @@ -104,27 +104,31 @@ namespace SqlSugar var tableWithString = inserable.InsertBuilder.TableWithString; var removeCacheFunc = inserable.RemoveCacheFunc; var objects = inserable.InsertObjs; - if (objects == null || objects.Count() == 0 || (objects.Count() == 1 && objects.First() == null)) + if (objects == null || objects.Count() == 0 || (objects.Count() == 1 && objects.First() == null)) { return result; } var identityList = inserable.EntityInfo.Columns.Where(it => it.IsIdentity).Select(it => it.PropertyName).ToArray(); - if (inserable.IsOffIdentity) + if (inserable.IsOffIdentity) { identityList = new string[] { }; } - this.Context.Utilities.PageEach(objects, 100, pagelist => + var pageSize = 100; + var count = inserable.EntityInfo.Columns.Count(); + pageSize = GetPageSize(pageSize, count); + this.Context.Utilities.PageEach(objects, pageSize, pagelist => { - - StringBuilder batchInsetrSql; - List allParamter=new List(); - GetInsertValues(identityList,columns, tableWithString, removeCacheFunc, pagelist, out batchInsetrSql, allParamter); - result += this.Context.Ado.ExecuteCommand(batchInsetrSql.ToString(), allParamter); + + StringBuilder batchInsetrSql; + List allParamter=new List(); + GetInsertValues(identityList,columns, tableWithString, removeCacheFunc, pagelist, out batchInsetrSql, allParamter); + result += this.Context.Ado.ExecuteCommand(batchInsetrSql.ToString(), allParamter); }); return result; } + public async Task ValuesExecuteCommandAsync() { int result = 0; @@ -150,6 +154,24 @@ namespace SqlSugar return result; } #region Values Helper + + private static int GetPageSize(int pageSize, int count) + { + if (pageSize * count > 2100) + { + pageSize = 50; + } + if (pageSize * count > 2100) + { + pageSize = 20; + } + if (pageSize * count > 2100) + { + pageSize = 10; + } + + return pageSize; + } private void GetInsertValues(string[] identitys, List columns, string tableWithString, Action removeCacheFunc, List items, out StringBuilder batchInsetrSql, List allParamter) { var itemable = this.Context.Insertable(items);