Fix tracked reference queries for SqlServer (#16020)
* Fixed issue with SqlServer and optimized queries to not do the actual paging if total count is 0 * Cleanup --------- Co-authored-by: Elitsa <elm@umbraco.dk>
This commit is contained in:
@@ -68,8 +68,7 @@ public interface ITrackedReferencesRepository
|
||||
long skip,
|
||||
long take,
|
||||
bool filterMustBeIsDependency,
|
||||
out long totalRecords) =>
|
||||
throw new NotImplementedException();
|
||||
out long totalRecords);
|
||||
|
||||
[Obsolete("Use overload that takes key instead of id. This will be removed in Umbraco 15.")]
|
||||
IEnumerable<RelationItemModel> GetPagedRelationsForItem(
|
||||
@@ -77,8 +76,7 @@ public interface ITrackedReferencesRepository
|
||||
long skip,
|
||||
long take,
|
||||
bool filterMustBeIsDependency,
|
||||
out long totalRecords) =>
|
||||
throw new NotImplementedException();
|
||||
out long totalRecords);
|
||||
|
||||
/// <summary>
|
||||
/// Gets a page of items used in any kind of relation from selected integer ids.
|
||||
|
||||
@@ -92,7 +92,7 @@ namespace Umbraco.Cms.Infrastructure.Persistence.Repositories.Implement
|
||||
}
|
||||
|
||||
var innerUnionSqlChild = _scopeAccessor.AmbientScope.Database.SqlContext.Sql().Select(
|
||||
"[cn].uniqueId as key", "[pn].uniqueId as otherKey, [cr].childId as id", "[cr].parentId as otherId", "[rt].[alias]", "[rt].[name]",
|
||||
"[cn].uniqueId as [key]", "[pn].uniqueId as otherKey, [cr].childId as id", "[cr].parentId as otherId", "[rt].[alias]", "[rt].[name]",
|
||||
"[rt].[isDependency]", "[rt].[dual]")
|
||||
.From<RelationDto>("cr")
|
||||
.InnerJoin<RelationTypeDto>("rt")
|
||||
@@ -103,7 +103,7 @@ namespace Umbraco.Cms.Infrastructure.Persistence.Repositories.Implement
|
||||
.On<RelationDto, NodeDto>((cr, pn) => cr.ParentId == pn.NodeId, "cr", "pn");
|
||||
|
||||
var innerUnionSqlDualParent = _scopeAccessor.AmbientScope.Database.SqlContext.Sql().Select(
|
||||
"[pn].uniqueId as key", "[cn].uniqueId as otherKey, [dpr].parentId as id", "[dpr].childId as otherId", "[dprt].[alias]", "[dprt].[name]",
|
||||
"[pn].uniqueId as [key]", "[cn].uniqueId as otherKey, [dpr].parentId as id", "[dpr].childId as otherId", "[dprt].[alias]", "[dprt].[name]",
|
||||
"[dprt].[isDependency]", "[dprt].[dual]")
|
||||
.From<RelationDto>("dpr")
|
||||
.InnerJoin<RelationTypeDto>("dprt")
|
||||
@@ -115,7 +115,7 @@ namespace Umbraco.Cms.Infrastructure.Persistence.Repositories.Implement
|
||||
.On<RelationDto, NodeDto>((dpr, pn) => dpr.ParentId == pn.NodeId, "dpr", "pn");
|
||||
|
||||
var innerUnionSql3 = _scopeAccessor.AmbientScope.Database.SqlContext.Sql().Select(
|
||||
"[cn].uniqueId as key", "[pn].uniqueId as otherKey, [dcr].childId as id", "[dcr].parentId as otherId", "[dcrt].[alias]", "[dcrt].[name]",
|
||||
"[cn].uniqueId as [key]", "[pn].uniqueId as otherKey, [dcr].childId as id", "[dcr].parentId as otherId", "[dcrt].[alias]", "[dcrt].[name]",
|
||||
"[dcrt].[isDependency]", "[dcrt].[dual]")
|
||||
.From<RelationDto>("dcr")
|
||||
.InnerJoin<RelationTypeDto>("dcrt")
|
||||
@@ -313,19 +313,32 @@ namespace Umbraco.Cms.Infrastructure.Persistence.Repositories.Implement
|
||||
aliasRight: "d")
|
||||
.Where<UnionHelperDto>(x => x.Key == key, "x");
|
||||
|
||||
|
||||
if (filterMustBeIsDependency)
|
||||
{
|
||||
sql = sql?.Where<RelationTypeDto>(rt => rt.IsDependency, "x");
|
||||
}
|
||||
|
||||
// Ordering is required for paging
|
||||
sql = sql?.OrderBy<RelationTypeDto>(x => x.Alias, "x");
|
||||
|
||||
RelationItemDto[] pagedResult =
|
||||
_scopeAccessor.AmbientScope?.Database.SkipTake<RelationItemDto>(skip, take, sql).ToArray() ??
|
||||
Array.Empty<RelationItemDto>();
|
||||
// find the count before ordering
|
||||
totalRecords = _scopeAccessor.AmbientScope?.Database.Count(sql!) ?? 0;
|
||||
|
||||
RelationItemDto[] pagedResult;
|
||||
|
||||
//Only to all this, if there is items
|
||||
if (totalRecords > 0)
|
||||
{
|
||||
// Ordering is required for paging
|
||||
sql = sql?.OrderBy<RelationTypeDto>(x => x.Alias, "x");
|
||||
|
||||
pagedResult =
|
||||
_scopeAccessor.AmbientScope?.Database.SkipTake<RelationItemDto>(skip, take, sql).ToArray() ??
|
||||
Array.Empty<RelationItemDto>();
|
||||
}
|
||||
else
|
||||
{
|
||||
pagedResult = Array.Empty<RelationItemDto>();
|
||||
}
|
||||
|
||||
return _umbracoMapper.MapEnumerable<RelationItemDto, RelationItemModel>(pagedResult);
|
||||
}
|
||||
|
||||
@@ -368,14 +381,26 @@ namespace Umbraco.Cms.Infrastructure.Persistence.Repositories.Implement
|
||||
sql = sql?.Where<RelationTypeDto>(rt => rt.IsDependency, "x");
|
||||
}
|
||||
|
||||
// Ordering is required for paging
|
||||
sql = sql?.OrderBy<RelationTypeDto>(x => x.Alias, "x");
|
||||
|
||||
RelationItemDto[] pagedResult =
|
||||
_scopeAccessor.AmbientScope?.Database.SkipTake<RelationItemDto>(skip, take, sql).ToArray() ??
|
||||
Array.Empty<RelationItemDto>();
|
||||
// find the count before ordering
|
||||
totalRecords = _scopeAccessor.AmbientScope?.Database.Count(sql!) ?? 0;
|
||||
|
||||
RelationItemDto[] pagedResult;
|
||||
|
||||
//Only to all this, if there is items
|
||||
if (totalRecords > 0)
|
||||
{
|
||||
// Ordering is required for paging
|
||||
sql = sql?.OrderBy<RelationTypeDto>(x => x.Alias, "x");
|
||||
|
||||
pagedResult =
|
||||
_scopeAccessor.AmbientScope?.Database.SkipTake<RelationItemDto>(skip, take, sql).ToArray() ??
|
||||
Array.Empty<RelationItemDto>();
|
||||
}
|
||||
else
|
||||
{
|
||||
pagedResult = Array.Empty<RelationItemDto>();
|
||||
}
|
||||
|
||||
return _umbracoMapper.MapEnumerable<RelationItemDto, RelationItemModel>(pagedResult);
|
||||
}
|
||||
|
||||
@@ -420,15 +445,26 @@ namespace Umbraco.Cms.Infrastructure.Persistence.Repositories.Implement
|
||||
sql = sql?.Where<RelationTypeDto>(rt => rt.IsDependency, "x");
|
||||
}
|
||||
|
||||
// Ordering is required for paging
|
||||
sql = sql?.OrderBy<RelationTypeDto>(x => x.Alias, "x");
|
||||
|
||||
RelationItemDto[] pagedResult =
|
||||
_scopeAccessor.AmbientScope?.Database.SkipTake<RelationItemDto>(skip, take, sql).ToArray() ??
|
||||
Array.Empty<RelationItemDto>();
|
||||
|
||||
// find the count before ordering
|
||||
totalRecords = _scopeAccessor.AmbientScope?.Database.Count(sql!) ?? 0;
|
||||
|
||||
RelationItemDto[] pagedResult;
|
||||
|
||||
//Only to all this, if there is items
|
||||
if (totalRecords > 0)
|
||||
{
|
||||
// Ordering is required for paging
|
||||
sql = sql?.OrderBy<RelationTypeDto>(x => x.Alias, "x");
|
||||
|
||||
pagedResult =
|
||||
_scopeAccessor.AmbientScope?.Database.SkipTake<RelationItemDto>(skip, take, sql).ToArray() ??
|
||||
Array.Empty<RelationItemDto>();
|
||||
}
|
||||
else
|
||||
{
|
||||
pagedResult = Array.Empty<RelationItemDto>();
|
||||
}
|
||||
|
||||
return _umbracoMapper.MapEnumerable<RelationItemDto, RelationItemModel>(pagedResult);
|
||||
}
|
||||
|
||||
@@ -487,14 +523,27 @@ namespace Umbraco.Cms.Infrastructure.Persistence.Repositories.Implement
|
||||
sql = sql?.Where<RelationTypeDto>(rt => rt.IsDependency, "x");
|
||||
}
|
||||
|
||||
// Ordering is required for paging
|
||||
sql = sql?.OrderBy<RelationTypeDto>(x => x.Alias, "x");
|
||||
|
||||
List<RelationItemDto>? pagedResult = _scopeAccessor.AmbientScope?.Database.SkipTake<RelationItemDto>(skip, take, sql);
|
||||
// find the count before ordering
|
||||
totalRecords = _scopeAccessor.AmbientScope?.Database.Count(sql!) ?? 0;
|
||||
|
||||
return _umbracoMapper.MapEnumerable<RelationItemDto, RelationItemModel>(pagedResult ??
|
||||
new List<RelationItemDto>());
|
||||
RelationItemDto[] pagedResult;
|
||||
|
||||
//Only to all this, if there is items
|
||||
if (totalRecords > 0)
|
||||
{
|
||||
// Ordering is required for paging
|
||||
sql = sql?.OrderBy<RelationTypeDto>(x => x.Alias, "x");
|
||||
|
||||
pagedResult =
|
||||
_scopeAccessor.AmbientScope?.Database.SkipTake<RelationItemDto>(skip, take, sql).ToArray() ??
|
||||
Array.Empty<RelationItemDto>();
|
||||
}
|
||||
else
|
||||
{
|
||||
pagedResult = Array.Empty<RelationItemDto>();
|
||||
}
|
||||
|
||||
return _umbracoMapper.MapEnumerable<RelationItemDto, RelationItemModel>(pagedResult);
|
||||
}
|
||||
|
||||
[Obsolete("Use overload that takes keys instead of ids. This will be removed in Umbraco 15.")]
|
||||
@@ -539,14 +588,26 @@ namespace Umbraco.Cms.Infrastructure.Persistence.Repositories.Implement
|
||||
sql = sql?.Where<RelationTypeDto>(rt => rt.IsDependency, "x");
|
||||
}
|
||||
|
||||
// Ordering is required for paging
|
||||
sql = sql?.OrderBy<RelationTypeDto>(x => x.Alias, "x");
|
||||
|
||||
RelationItemDto[] pagedResult =
|
||||
_scopeAccessor.AmbientScope?.Database.SkipTake<RelationItemDto>(skip, take, sql).ToArray() ??
|
||||
Array.Empty<RelationItemDto>();
|
||||
// find the count before ordering
|
||||
totalRecords = _scopeAccessor.AmbientScope?.Database.Count(sql!) ?? 0;
|
||||
|
||||
RelationItemDto[] pagedResult;
|
||||
|
||||
//Only to all this, if there is items
|
||||
if (totalRecords > 0)
|
||||
{
|
||||
// Ordering is required for paging
|
||||
sql = sql?.OrderBy<RelationTypeDto>(x => x.Alias, "x");
|
||||
|
||||
pagedResult =
|
||||
_scopeAccessor.AmbientScope?.Database.SkipTake<RelationItemDto>(skip, take, sql).ToArray() ??
|
||||
Array.Empty<RelationItemDto>();
|
||||
}
|
||||
else
|
||||
{
|
||||
pagedResult = Array.Empty<RelationItemDto>();
|
||||
}
|
||||
|
||||
return _umbracoMapper.MapEnumerable<RelationItemDto, RelationItemModel>(pagedResult);
|
||||
}
|
||||
|
||||
@@ -603,15 +664,27 @@ namespace Umbraco.Cms.Infrastructure.Persistence.Repositories.Implement
|
||||
sql = sql?.Where<RelationTypeDto>(rt => rt.IsDependency, "x");
|
||||
}
|
||||
|
||||
// Ordering is required for paging
|
||||
sql = sql?.OrderBy<RelationTypeDto>(x => x.Alias, "x");
|
||||
|
||||
List<RelationItemDto>? pagedResult =
|
||||
_scopeAccessor.AmbientScope?.Database.SkipTake<RelationItemDto>(skip, take, sql);
|
||||
// find the count before ordering
|
||||
totalRecords = _scopeAccessor.AmbientScope?.Database.Count(sql!) ?? 0;
|
||||
|
||||
return _umbracoMapper.MapEnumerable<RelationItemDto, RelationItemModel>(pagedResult ??
|
||||
new List<RelationItemDto>());
|
||||
RelationItemDto[] pagedResult;
|
||||
|
||||
//Only to all this, if there is items
|
||||
if (totalRecords > 0)
|
||||
{
|
||||
// Ordering is required for paging
|
||||
sql = sql?.OrderBy<RelationTypeDto>(x => x.Alias, "x");
|
||||
|
||||
pagedResult =
|
||||
_scopeAccessor.AmbientScope?.Database.SkipTake<RelationItemDto>(skip, take, sql).ToArray() ??
|
||||
Array.Empty<RelationItemDto>();
|
||||
}
|
||||
else
|
||||
{
|
||||
pagedResult = Array.Empty<RelationItemDto>();
|
||||
}
|
||||
|
||||
return _umbracoMapper.MapEnumerable<RelationItemDto, RelationItemModel>(pagedResult);
|
||||
}
|
||||
|
||||
private class UnionHelperDto
|
||||
|
||||
@@ -77,7 +77,8 @@ internal static class UmbracoDatabaseExtensions
|
||||
|
||||
public static long Count(this IUmbracoDatabase database, Sql sql)
|
||||
{
|
||||
var query = new Sql().Select("COUNT(*)").From().Append("(").Append(sql).Append(")");
|
||||
// We need to copy the sql into a new object, to avoid this method from changing the sql.
|
||||
var query = new Sql().Select("COUNT(*)").From().Append("(").Append(new Sql(sql.SQL, sql.Arguments)).Append(") as count_query");
|
||||
|
||||
return database.ExecuteScalar<long>(query);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user