From 7d6e9a3d54f3347e8517315712362fe5db863a8f Mon Sep 17 00:00:00 2001 From: nzdev <834725+nzdev@users.noreply.github.com> Date: Mon, 7 Dec 2020 23:09:16 +1300 Subject: [PATCH 1/5] Speed up deleting relationship by parent. --- .../Implement/RelationRepository.cs | 20 +++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/Umbraco.Core/Persistence/Repositories/Implement/RelationRepository.cs b/src/Umbraco.Core/Persistence/Repositories/Implement/RelationRepository.cs index 56a6336f75..ab699fd5f1 100644 --- a/src/Umbraco.Core/Persistence/Repositories/Implement/RelationRepository.cs +++ b/src/Umbraco.Core/Persistence/Repositories/Implement/RelationRepository.cs @@ -286,17 +286,21 @@ namespace Umbraco.Core.Persistence.Repositories.Implement public void DeleteByParent(int parentId, params string[] relationTypeAliases) { - var subQuery = Sql().Select(x => x.Id) - .From() - .InnerJoin().On(x => x.RelationType, x => x.Id) - .Where(x => x.ParentId == parentId); - if (relationTypeAliases.Length > 0) { - subQuery.WhereIn(x => x.Alias, relationTypeAliases); + var query = Sql().Delete() + .InnerJoin().On(x => x.RelationType, x => x.Id) + .Where(x => x.ParentId == parentId); + Database.Execute(query); + } + else + { + var query = Sql().Delete() + .InnerJoin().On(x => x.RelationType, x => x.Id) + .Where(x => x.ParentId == parentId) + .WhereIn(x => x.Alias, relationTypeAliases); + Database.Execute(query); } - - Database.Execute(Sql().Delete().WhereIn(x => x.Id, subQuery)); } /// From 7bd331d97bbadcd31ba584a64cde3940b6ffe592 Mon Sep 17 00:00:00 2001 From: nzdev <834725+nzdev@users.noreply.github.com> Date: Mon, 7 Dec 2020 23:14:42 +1300 Subject: [PATCH 2/5] Add from --- .../Persistence/Repositories/Implement/RelationRepository.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Umbraco.Core/Persistence/Repositories/Implement/RelationRepository.cs b/src/Umbraco.Core/Persistence/Repositories/Implement/RelationRepository.cs index ab699fd5f1..11ac96c7c6 100644 --- a/src/Umbraco.Core/Persistence/Repositories/Implement/RelationRepository.cs +++ b/src/Umbraco.Core/Persistence/Repositories/Implement/RelationRepository.cs @@ -289,6 +289,7 @@ namespace Umbraco.Core.Persistence.Repositories.Implement if (relationTypeAliases.Length > 0) { var query = Sql().Delete() + .From() .InnerJoin().On(x => x.RelationType, x => x.Id) .Where(x => x.ParentId == parentId); Database.Execute(query); @@ -296,6 +297,7 @@ namespace Umbraco.Core.Persistence.Repositories.Implement else { var query = Sql().Delete() + .From() .InnerJoin().On(x => x.RelationType, x => x.Id) .Where(x => x.ParentId == parentId) .WhereIn(x => x.Alias, relationTypeAliases); From 38f748c5638407600cdcc54c5424e6d0a858c95d Mon Sep 17 00:00:00 2001 From: nzdev <834725+nzdev@users.noreply.github.com> Date: Mon, 7 Dec 2020 23:27:57 +1300 Subject: [PATCH 3/5] correct ordering of statements, use sqltemplates --- src/Umbraco.Core/Constants-SqlTemplates.cs | 6 +++++- .../Implement/RelationRepository.cs | 19 ++++++++++--------- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/src/Umbraco.Core/Constants-SqlTemplates.cs b/src/Umbraco.Core/Constants-SqlTemplates.cs index 984bc495b0..a1d0d5c3ea 100644 --- a/src/Umbraco.Core/Constants-SqlTemplates.cs +++ b/src/Umbraco.Core/Constants-SqlTemplates.cs @@ -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"; + } } } } diff --git a/src/Umbraco.Core/Persistence/Repositories/Implement/RelationRepository.cs b/src/Umbraco.Core/Persistence/Repositories/Implement/RelationRepository.cs index 11ac96c7c6..a0dbea5944 100644 --- a/src/Umbraco.Core/Persistence/Repositories/Implement/RelationRepository.cs +++ b/src/Umbraco.Core/Persistence/Repositories/Implement/RelationRepository.cs @@ -288,20 +288,21 @@ namespace Umbraco.Core.Persistence.Repositories.Implement { if (relationTypeAliases.Length > 0) { - var query = Sql().Delete() - .From() - .InnerJoin().On(x => x.RelationType, x => x.Id) - .Where(x => x.ParentId == parentId); - Database.Execute(query); + var template = SqlContext.Templates.Get(Constants.SqlTemplates.RelationRepository.DeleteByParentIn, tsql => Sql().Delete() + .From() + .InnerJoin().On(x => x.RelationType, x => x.Id) + .Where(x => x.ParentId == SqlTemplate.Arg(nameof(parentId))) + .WhereIn(x => x.Alias, SqlTemplate.ArgIn(nameof(relationTypeAliases)))); + Database.Execute(template.Sql(new { parentId, relationTypeAliases })); } else { - var query = Sql().Delete() + + var template = SqlContext.Templates.Get(Constants.SqlTemplates.RelationRepository.DeleteByParentAll, tsql => Sql().Delete() .From() .InnerJoin().On(x => x.RelationType, x => x.Id) - .Where(x => x.ParentId == parentId) - .WhereIn(x => x.Alias, relationTypeAliases); - Database.Execute(query); + .Where(x => x.ParentId == SqlTemplate.Arg(nameof(parentId)))); + Database.Execute(template.Sql(new { parentId })); } } From 39b1de4a98c5d009bb607370fb6dc2590698b1aa Mon Sep 17 00:00:00 2001 From: nzdev <834725+nzdev@users.noreply.github.com> Date: Mon, 7 Dec 2020 23:29:13 +1300 Subject: [PATCH 4/5] avoid capture class --- .../Repositories/Implement/RelationRepository.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Umbraco.Core/Persistence/Repositories/Implement/RelationRepository.cs b/src/Umbraco.Core/Persistence/Repositories/Implement/RelationRepository.cs index a0dbea5944..eab77ffbc6 100644 --- a/src/Umbraco.Core/Persistence/Repositories/Implement/RelationRepository.cs +++ b/src/Umbraco.Core/Persistence/Repositories/Implement/RelationRepository.cs @@ -291,8 +291,8 @@ namespace Umbraco.Core.Persistence.Repositories.Implement var template = SqlContext.Templates.Get(Constants.SqlTemplates.RelationRepository.DeleteByParentIn, tsql => Sql().Delete() .From() .InnerJoin().On(x => x.RelationType, x => x.Id) - .Where(x => x.ParentId == SqlTemplate.Arg(nameof(parentId))) - .WhereIn(x => x.Alias, SqlTemplate.ArgIn(nameof(relationTypeAliases)))); + .Where(x => x.ParentId == SqlTemplate.Arg("parentId")) + .WhereIn(x => x.Alias, SqlTemplate.ArgIn("relationTypeAliases"))); Database.Execute(template.Sql(new { parentId, relationTypeAliases })); } else @@ -301,7 +301,7 @@ namespace Umbraco.Core.Persistence.Repositories.Implement var template = SqlContext.Templates.Get(Constants.SqlTemplates.RelationRepository.DeleteByParentAll, tsql => Sql().Delete() .From() .InnerJoin().On(x => x.RelationType, x => x.Id) - .Where(x => x.ParentId == SqlTemplate.Arg(nameof(parentId)))); + .Where(x => x.ParentId == SqlTemplate.Arg("parentId"))); Database.Execute(template.Sql(new { parentId })); } } From ec364097f14e163b90b0f09807f5e8184d1ab3ac Mon Sep 17 00:00:00 2001 From: nzdev <834725+nzdev@users.noreply.github.com> Date: Tue, 8 Dec 2020 10:26:52 +1300 Subject: [PATCH 5/5] Revert use of sqltemplates. Use previous query for sqlce --- .../Implement/RelationRepository.cs | 44 +++++++++++++------ 1 file changed, 31 insertions(+), 13 deletions(-) diff --git a/src/Umbraco.Core/Persistence/Repositories/Implement/RelationRepository.cs b/src/Umbraco.Core/Persistence/Repositories/Implement/RelationRepository.cs index eab77ffbc6..e8c66e1fa6 100644 --- a/src/Umbraco.Core/Persistence/Repositories/Implement/RelationRepository.cs +++ b/src/Umbraco.Core/Persistence/Repositories/Implement/RelationRepository.cs @@ -286,23 +286,41 @@ namespace Umbraco.Core.Persistence.Repositories.Implement public void DeleteByParent(int parentId, params string[] relationTypeAliases) { - if (relationTypeAliases.Length > 0) + if (Database.DatabaseType.IsSqlCe()) { - var template = SqlContext.Templates.Get(Constants.SqlTemplates.RelationRepository.DeleteByParentIn, tsql => Sql().Delete() - .From() - .InnerJoin().On(x => x.RelationType, x => x.Id) - .Where(x => x.ParentId == SqlTemplate.Arg("parentId")) - .WhereIn(x => x.Alias, SqlTemplate.ArgIn("relationTypeAliases"))); - Database.Execute(template.Sql(new { parentId, relationTypeAliases })); + var subQuery = Sql().Select(x => x.Id) + .From() + .InnerJoin().On(x => x.RelationType, x => x.Id) + .Where(x => x.ParentId == parentId); + + if (relationTypeAliases.Length > 0) + { + subQuery.WhereIn(x => x.Alias, relationTypeAliases); + } + + Database.Execute(Sql().Delete().WhereIn(x => x.Id, subQuery)); + } else { - - var template = SqlContext.Templates.Get(Constants.SqlTemplates.RelationRepository.DeleteByParentAll, tsql => Sql().Delete() - .From() - .InnerJoin().On(x => x.RelationType, x => x.Id) - .Where(x => x.ParentId == SqlTemplate.Arg("parentId"))); - Database.Execute(template.Sql(new { parentId })); + if (relationTypeAliases.Length > 0) + { + var sql = Sql().Delete() + .From() + .InnerJoin().On(x => x.RelationType, x => x.Id) + .Where(x => x.ParentId == parentId) + .WhereIn(x => x.Alias, relationTypeAliases); + Database.Execute(sql); + } + else + { + + var sql = Sql().Delete() + .From() + .InnerJoin().On(x => x.RelationType, x => x.Id) + .Where(x => x.ParentId == parentId); + Database.Execute(sql); + } } }