Skip to content

Commit

Permalink
- 优化 DbSet/Repository 删除多主键分批300,防止 SQL AND OR 过长问题;
Browse files Browse the repository at this point in the history
  • Loading branch information
2881099 committed Nov 5, 2024
1 parent 01a0bfd commit fa0e5ba
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 7 deletions.
11 changes: 10 additions & 1 deletion FreeSql.DbContext/DbSet/DbSetAsync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,16 @@ async Task UpdateRangePrivAsync(IEnumerable<TEntity> data, bool isCheck, Cancell
async Task<int> DbContextBatchRemoveAsync(EntityState[] dels, CancellationToken cancellationToken)
{
if (dels.Any() == false) return 0;
var affrows = await this.OrmDelete(dels.Select(a => a.Value)).ExecuteAffrowsAsync(cancellationToken);
var affrows = 0;
if (_table.Primarys.Length == 1)
affrows = await this.OrmDelete(dels.Select(a => a.Value)).ExecuteAffrowsAsync(cancellationToken);
else
{
var takeMax = 300;
var execCount = (int)Math.Ceiling(1.0 * dels.Length / takeMax);
for (var a = 0; a < execCount; a++)
affrows += await this.OrmDelete(dels.Skip(a * takeMax).Take(Math.Min(takeMax, dels.Length - a * takeMax)).Select(d => d.Value)).ExecuteAffrowsAsync(cancellationToken);
}
_db._entityChangeReport.AddRange(dels.Select(a => new DbContext.EntityChangeReport.ChangeInfo { EntityType = _entityType, Object = a.Value, Type = DbContext.EntityChangeType.Delete }));
return affrows;
}
Expand Down
11 changes: 10 additions & 1 deletion FreeSql.DbContext/DbSet/DbSetSync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,16 @@ void UpdateRangePriv(IEnumerable<TEntity> data, bool isCheck)
int DbContextBatchRemove(EntityState[] dels)
{
if (dels.Any() == false) return 0;
var affrows = this.OrmDelete(dels.Select(a => a.Value)).ExecuteAffrows();
var affrows = 0;
if (_table.Primarys.Length == 1)
affrows = this.OrmDelete(dels.Select(a => a.Value)).ExecuteAffrows();
else
{
var takeMax = 300;
var execCount = (int)Math.Ceiling(1.0 * dels.Length / takeMax);
for (var a = 0; a < execCount; a++)
affrows += this.OrmDelete(dels.Skip(a * takeMax).Take(Math.Min(takeMax, dels.Length - a * takeMax)).Select(d => d.Value)).ExecuteAffrows();
}
_db._entityChangeReport.AddRange(dels.Select(a => new DbContext.EntityChangeReport.ChangeInfo { EntityType = _entityType, Object = a.Value, Type = DbContext.EntityChangeType.Delete }));
return affrows; //https://github.com/dotnetcore/FreeSql/issues/373
}
Expand Down
8 changes: 8 additions & 0 deletions FreeSql.DbContext/FreeSql.DbContext.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 7 additions & 4 deletions FreeSql/Internal/CommonExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1347,10 +1347,6 @@ public string ExpressionLambdaToSql(Expression exp, ExpTSC tsc)
break;
}
break;
case "ToOne":
case "First":
fsqlSelect0._limit = 1; //#462
break;
}
if (tsc.dbParams != null) fsqlSelect0._params = tsc.dbParams;
fsqltables = fsqlSelect0._tables;
Expand Down Expand Up @@ -1774,6 +1770,13 @@ public string ExpressionLambdaToSql(Expression exp, ExpTSC tsc)
case "ToList":
case "ToOne":
case "First":
switch (exp3.Method.Name)
{
case "ToOne":
case "First":
fsqlSelect0._limit = 1; //#462
break;
}
var tscClone2 = tsc.CloneDisableDiyParse();
var fsqlSelect0p = fsql as Select0Provider;
tscClone2.subSelect001 = fsqlSelect0p; //#405 Oracle within group(order by ..)
Expand Down
1 change: 0 additions & 1 deletion FreeSql/Internal/CommonUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,6 @@ object LocalGetSingleElement(IEnumerable ie)
}
if (dywhere is IEnumerable)
{

var sb = new StringBuilder();
var ie = dywhere as IEnumerable;
var ieSingle = LocalGetSingleElement(ie);
Expand Down

0 comments on commit fa0e5ba

Please sign in to comment.