Skip to content

Commit

Permalink
Fixing regression bug introduced in umbraco#14806 and found during in…
Browse files Browse the repository at this point in the history
…vestigation of umbraco#16803 - well tested on db with 300k+ nodes
  • Loading branch information
Wojciech Tengler committed Jul 29, 2024
1 parent b14ac81 commit 2f64847
Showing 1 changed file with 30 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -221,10 +221,17 @@ public IEnumerable<ContentNodeKit> GetAllContentSources()
.Append(SqlObjectTypeNotTrashed(SqlContext, Constants.ObjectTypes.Document))
.Append(SqlOrderByLevelIdSortOrder(SqlContext));

// Use a more efficient COUNT query
Sql<ISqlContext>? sqlCountQuery = SqlContentSourcesCount()
.Append(SqlObjectTypeNotTrashed(SqlContext, Constants.ObjectTypes.Document));

Sql<ISqlContext>? sqlCount =
SqlContext.Sql("SELECT COUNT(*) FROM (").Append(sqlCountQuery).Append(") npoco_tbl");

IContentCacheDataSerializer serializer =
_contentCacheDataSerializerFactory.Create(ContentCacheDataSerializerEntityType.Document);

IEnumerable<ContentSourceDto> dtos = GetContentNodeDtos(sql);
IEnumerable<ContentSourceDto> dtos = GetContentNodeDtos(sql, sqlCount);

foreach (ContentSourceDto row in dtos)
{
Expand All @@ -239,10 +246,18 @@ public IEnumerable<ContentNodeKit> GetBranchContentSources(int id)
.Append(SqlWhereNodeIdX(SqlContext, id))
.Append(SqlOrderByLevelIdSortOrder(SqlContext));

// Use a more efficient COUNT query
Sql<ISqlContext>? sqlCountQuery = SqlContentSourcesCount(SqlContentSourcesSelectUmbracoNodeJoin)
.Append(SqlObjectTypeNotTrashed(SqlContext, Constants.ObjectTypes.Document))
.Append(SqlWhereNodeIdX(SqlContext, id));

Sql<ISqlContext>? sqlCount =
SqlContext.Sql("SELECT COUNT(*) FROM (").Append(sqlCountQuery).Append(") npoco_tbl");

IContentCacheDataSerializer serializer =
_contentCacheDataSerializerFactory.Create(ContentCacheDataSerializerEntityType.Document);

IEnumerable<ContentSourceDto> dtos = GetContentNodeDtos(sql);
IEnumerable<ContentSourceDto> dtos = GetContentNodeDtos(sql, sqlCount);

foreach (ContentSourceDto row in dtos)
{
Expand All @@ -262,10 +277,18 @@ public IEnumerable<ContentNodeKit> GetTypeContentSources(IEnumerable<int>? ids)
.WhereIn<ContentDto>(x => x.ContentTypeId, ids)
.Append(SqlOrderByLevelIdSortOrder(SqlContext));

// Use a more efficient COUNT query
Sql<ISqlContext> sqlCountQuery = SqlContentSourcesCount()
.Append(SqlObjectTypeNotTrashed(SqlContext, Constants.ObjectTypes.Document))
.WhereIn<ContentDto>(x => x.ContentTypeId, ids);

Sql<ISqlContext>? sqlCount =
SqlContext.Sql("SELECT COUNT(*) FROM (").Append(sqlCountQuery).Append(") npoco_tbl");

IContentCacheDataSerializer serializer =
_contentCacheDataSerializerFactory.Create(ContentCacheDataSerializerEntityType.Document);

IEnumerable<ContentSourceDto> dtos = GetContentNodeDtos(sql);
IEnumerable<ContentSourceDto> dtos = GetContentNodeDtos(sql, sqlCount);

foreach (ContentSourceDto row in dtos)
{
Expand Down Expand Up @@ -1015,27 +1038,14 @@ private IEnumerable<ContentSourceDto> GetMediaNodeDtos(Sql<ISqlContext> sql)
return dtos;
}

private IEnumerable<ContentSourceDto> GetContentNodeDtos(Sql<ISqlContext> sql)
private IEnumerable<ContentSourceDto> GetContentNodeDtos(Sql<ISqlContext> sql, Sql<ISqlContext> sqlCount)
{
// We need to page here. We don't want to iterate over every single row in one connection cuz this can cause an SQL Timeout.
// We also want to read with a db reader and not load everything into memory, QueryPaged lets us do that.
// QueryPaged is very slow on large sites however, so use fetch if UsePagedSqlQuery is disabled.
IEnumerable<ContentSourceDto> dtos;
if (_nucacheSettings.Value.UsePagedSqlQuery)
{
// Use a more efficient COUNT query
Sql<ISqlContext>? sqlCountQuery = SqlContentSourcesCount()
.Append(SqlObjectTypeNotTrashed(SqlContext, Constants.ObjectTypes.Document));

Sql<ISqlContext>? sqlCount =
SqlContext.Sql("SELECT COUNT(*) FROM (").Append(sqlCountQuery).Append(") npoco_tbl");

dtos = Database.QueryPaged<ContentSourceDto>(_nucacheSettings.Value.SqlPageSize, sql, sqlCount);
}
else
{
dtos = Database.Fetch<ContentSourceDto>(sql);
}
IEnumerable<ContentSourceDto> dtos = _nucacheSettings.Value.UsePagedSqlQuery ?
Database.QueryPaged<ContentSourceDto>(_nucacheSettings.Value.SqlPageSize, sql, sqlCount) :
Database.Fetch<ContentSourceDto>(sql);

return dtos;
}
Expand Down

0 comments on commit 2f64847

Please sign in to comment.