Merge pull request #9508 from nzdev/v8/feature/faster-relationship-delete-sql

Speed up deleting relationship by parent. (Speeds up publishing/ saving)
This commit is contained in:
Shannon Deminick
2020-12-16 12:28:27 +11:00
committed by GitHub
2 changed files with 39 additions and 10 deletions

View File

@@ -14,7 +14,11 @@
public const string GetParentNode = "Umbraco.Core.VersionableRepository.GetParentNode";
public const string GetReservedId = "Umbraco.Core.VersionableRepository.GetReservedId";
}
public static class RelationRepository
{
public const string DeleteByParentAll = "Umbraco.Core.RelationRepository.DeleteByParent";
public const string DeleteByParentIn = "Umbraco.Core.RelationRepository.DeleteByParentIn";
}
}
}
}

View File

@@ -286,17 +286,42 @@ namespace Umbraco.Core.Persistence.Repositories.Implement
public void DeleteByParent(int parentId, params string[] relationTypeAliases)
{
var subQuery = Sql().Select<RelationDto>(x => x.Id)
.From<RelationDto>()
.InnerJoin<RelationTypeDto>().On<RelationDto, RelationTypeDto>(x => x.RelationType, x => x.Id)
.Where<RelationDto>(x => x.ParentId == parentId);
if (relationTypeAliases.Length > 0)
if (Database.DatabaseType.IsSqlCe())
{
subQuery.WhereIn<RelationTypeDto>(x => x.Alias, relationTypeAliases);
}
var subQuery = Sql().Select<RelationDto>(x => x.Id)
.From<RelationDto>()
.InnerJoin<RelationTypeDto>().On<RelationDto, RelationTypeDto>(x => x.RelationType, x => x.Id)
.Where<RelationDto>(x => x.ParentId == parentId);
Database.Execute(Sql().Delete<RelationDto>().WhereIn<RelationDto>(x => x.Id, subQuery));
if (relationTypeAliases.Length > 0)
{
subQuery.WhereIn<RelationTypeDto>(x => x.Alias, relationTypeAliases);
}
Database.Execute(Sql().Delete<RelationDto>().WhereIn<RelationDto>(x => x.Id, subQuery));
}
else
{
if (relationTypeAliases.Length > 0)
{
var sql = Sql().Delete<RelationDto>()
.From<RelationDto>()
.InnerJoin<RelationTypeDto>().On<RelationDto, RelationTypeDto>(x => x.RelationType, x => x.Id)
.Where<RelationDto>(x => x.ParentId == parentId)
.WhereIn<RelationTypeDto>(x => x.Alias, relationTypeAliases);
Database.Execute(sql);
}
else
{
var sql = Sql().Delete<RelationDto>()
.From<RelationDto>()
.InnerJoin<RelationTypeDto>().On<RelationDto, RelationTypeDto>(x => x.RelationType, x => x.Id)
.Where<RelationDto>(x => x.ParentId == parentId);
Database.Execute(sql);
}
}
}
/// <summary>