diff --git a/src/Umbraco.Core/Persistence/Repositories/ContentRepository.cs b/src/Umbraco.Core/Persistence/Repositories/ContentRepository.cs index 84caeb2a3e..d989edebb8 100644 --- a/src/Umbraco.Core/Persistence/Repositories/ContentRepository.cs +++ b/src/Umbraco.Core/Persistence/Repositories/ContentRepository.cs @@ -104,6 +104,12 @@ namespace Umbraco.Core.Persistence.Repositories #endregion + #region Static Queries + + private readonly IQuery _publishedQuery = Query.Builder.Where(x => x.Published == true); + + #endregion + #region Overrides of PetaPocoRepositoryBase @@ -207,8 +213,7 @@ namespace Umbraco.Core.Persistence.Repositories //now insert the data, again if something fails here, the whole transaction is reversed if (contentTypeIds == null) { - var query = Query.Builder.Where(x => x.Published == true); - RebuildXmlStructuresProcessQuery(serializer, query, tr, groupSize); + RebuildXmlStructuresProcessQuery(serializer, _publishedQuery, tr, groupSize); } else { diff --git a/src/Umbraco.Core/Persistence/Repositories/RepositoryBase.cs b/src/Umbraco.Core/Persistence/Repositories/RepositoryBase.cs index 5534a9ea40..9379d14af8 100644 --- a/src/Umbraco.Core/Persistence/Repositories/RepositoryBase.cs +++ b/src/Umbraco.Core/Persistence/Repositories/RepositoryBase.cs @@ -83,6 +83,12 @@ namespace Umbraco.Core.Persistence.Repositories } + #region Static Queries + + private readonly IQuery _hasIdQuery = Query.Builder.Where(x => x.Id != 0); + + #endregion + protected virtual TId GetEntityId(TEntity entity) { return (TId)(object)entity.Id; @@ -112,8 +118,7 @@ namespace Umbraco.Core.Persistence.Repositories new RepositoryCachePolicyOptions(() => { //Get count of all entities of current type (TEntity) to ensure cached result is correct - var query = Query.Builder.Where(x => x.Id != 0); - return PerformCount(query); + return PerformCount(_hasIdQuery); }))); } } diff --git a/src/Umbraco.Core/Services/ContentService.cs b/src/Umbraco.Core/Services/ContentService.cs index 38b7aec9d7..6223e5933b 100644 --- a/src/Umbraco.Core/Services/ContentService.cs +++ b/src/Umbraco.Core/Services/ContentService.cs @@ -56,6 +56,12 @@ namespace Umbraco.Core.Services _userService = userService; } + #region Static Queries + + private readonly IQuery _notTrashedQuery = Query.Builder.Where(x => x.Trashed == false); + + #endregion + public int CountPublished(string contentTypeAlias = null) { var uow = UowProvider.GetUnitOfWork(); @@ -789,8 +795,7 @@ namespace Umbraco.Core.Services { using (var repository = RepositoryFactory.CreateContentRepository(UowProvider.GetUnitOfWork())) { - var query = Query.Builder.Where(x => x.Trashed == false); - return repository.GetByPublishedVersion(query); + return repository.GetByPublishedVersion(_notTrashedQuery); } } diff --git a/src/Umbraco.Core/Services/EntityService.cs b/src/Umbraco.Core/Services/EntityService.cs index 257416d054..a6b84704fc 100644 --- a/src/Umbraco.Core/Services/EntityService.cs +++ b/src/Umbraco.Core/Services/EntityService.cs @@ -62,6 +62,12 @@ namespace Umbraco.Core.Services } + #region Static Queries + + private readonly IQuery _rootEntityQuery = Query.Builder.Where(x => x.ParentId == -1); + + #endregion + /// /// Returns the integer id for a given GUID /// @@ -389,8 +395,7 @@ namespace Umbraco.Core.Services var objectTypeId = umbracoObjectType.GetGuid(); using (var repository = RepositoryFactory.CreateEntityRepository(UowProvider.GetUnitOfWork())) { - var query = Query.Builder.Where(x => x.ParentId == -1); - var entities = repository.GetByQuery(query, objectTypeId); + var entities = repository.GetByQuery(_rootEntityQuery, objectTypeId); return entities; } diff --git a/src/Umbraco.Tests.Benchmarks/ModelToSqlExpressionHelperBenchmarks.cs b/src/Umbraco.Tests.Benchmarks/ModelToSqlExpressionHelperBenchmarks.cs index 922badc33b..2b1561012b 100644 --- a/src/Umbraco.Tests.Benchmarks/ModelToSqlExpressionHelperBenchmarks.cs +++ b/src/Umbraco.Tests.Benchmarks/ModelToSqlExpressionHelperBenchmarks.cs @@ -51,9 +51,7 @@ namespace Umbraco.Tests.Benchmarks private readonly ISqlSyntaxProvider _syntaxProvider = new SqlCeSyntaxProvider(); private readonly BaseMapper _contentMapper; private readonly CachedExpression _cachedExpression; - //private static readonly Expression> TemplatePredicate = content => - // content.Path.StartsWith(string.Empty) && content.Published && (content.ContentTypeId == 0 || content.ContentTypeId == 0); - + [Benchmark(Baseline = true)] public void WithNonCached() { diff --git a/src/UmbracoExamine/UmbracoContentIndexer.cs b/src/UmbracoExamine/UmbracoContentIndexer.cs index 4fcf51deae..f69e7e59bb 100644 --- a/src/UmbracoExamine/UmbracoContentIndexer.cs +++ b/src/UmbracoExamine/UmbracoContentIndexer.cs @@ -347,6 +347,11 @@ namespace UmbracoExamine #region Protected + /// + /// This is a static query, it's parameters don't change so store statically + /// + private static readonly IQuery PublishedQuery = Query.Builder.Where(x => x.Published == true); + protected override void PerformIndexAll(string type) { const int pageSize = 10000; @@ -376,9 +381,7 @@ namespace UmbracoExamine else { //add the published filter - var qry = Query.Builder.Where(x => x.Published == true); - - descendants = _contentService.GetPagedDescendants(contentParentId, pageIndex, pageSize, out total, "Path", Direction.Ascending, true, qry); + descendants = _contentService.GetPagedDescendants(contentParentId, pageIndex, pageSize, out total, "Path", Direction.Ascending, true, PublishedQuery); } //if specific types are declared we need to post filter them