Optimizing Count() usage where needed.

Prevents useless queries when huge amount of data is manipulated.
This commit is contained in:
Sebastien Ros
2012-07-13 11:24:06 -07:00
parent 65eed0a23f
commit 9a716ada30
6 changed files with 19 additions and 15 deletions

View File

@@ -154,7 +154,7 @@ namespace Orchard.Blogs.Controllers {
list.AddRange(_blogService.Get(VersionOptions.Latest)
.Select(b => {
var blog = Services.ContentManager.BuildDisplay(b, "SummaryAdmin");
blog.TotalPostCount = _blogPostService.Get(b, VersionOptions.Latest).Count();
blog.TotalPostCount = _blogPostService.PostCount(b, VersionOptions.Latest);
return blog;
}));

View File

@@ -32,7 +32,7 @@ namespace Orchard.Blogs.Handlers {
var posts = blogPostService.Get(blogPostPart.BlogPart, VersionOptions.Published);
// create a dictionary of all the year/month combinations and their count of posts that are published in this blog
var inMemoryBlogArchives = new Dictionary<DateTime, int>(posts.Count());
var inMemoryBlogArchives = new Dictionary<DateTime, int>();
foreach (var post in posts) {
if (!post.Has<CommonPart>())
continue;

View File

@@ -43,7 +43,7 @@ namespace Orchard.Blogs.Handlers {
// Ensure the "right" set of published posts for the blog is obtained
blogPart.ContentItem.ContentManager.Flush();
blogPart.PostCount = _blogPostService.Get(blogPart, VersionOptions.Published).Count();
blogPart.PostCount = _blogPostService.PostCount(blogPart);
}
}

View File

@@ -49,26 +49,26 @@ namespace Orchard.Comments.Controllers {
options = new CommentIndexOptions();
// Filtering
IContentQuery<CommentPart, CommentPartRecord> comments;
IContentQuery<CommentPart, CommentPartRecord> commentsQuery;
switch (options.Filter) {
case CommentIndexFilter.All:
comments = _commentService.GetComments();
commentsQuery = _commentService.GetComments();
break;
case CommentIndexFilter.Approved:
comments = _commentService.GetComments(CommentStatus.Approved);
commentsQuery = _commentService.GetComments(CommentStatus.Approved);
break;
case CommentIndexFilter.Pending:
comments = _commentService.GetComments(CommentStatus.Pending);
commentsQuery = _commentService.GetComments(CommentStatus.Pending);
break;
case CommentIndexFilter.Spam:
comments = _commentService.GetComments(CommentStatus.Spam);
commentsQuery = _commentService.GetComments(CommentStatus.Spam);
break;
default:
throw new ArgumentOutOfRangeException();
}
var pagerShape = Shape.Pager(pager).TotalItemCount(comments.Count());
var entries = comments
var pagerShape = Shape.Pager(pager).TotalItemCount(commentsQuery.Count());
var entries = commentsQuery
.OrderByDescending<CommentPartRecord, DateTime?>(cpr => cpr.CommentDateUtc)
.Slice(pager.GetStartIndex(), pager.PageSize)
.ToList()

View File

@@ -142,7 +142,10 @@ namespace Orchard.Tags.Services {
}
public int GetTaggedContentItemCount(int tagId, VersionOptions options) {
return GetTaggedContentItems(tagId, options).Count();
return _orchardServices.ContentManager
.Query<TagsPart, TagsPartRecord>()
.Where(tpr => tpr.Tags.Any(tr => tr.TagRecord.Id == tagId))
.Count();
}
private void TagContentItem(TagsPartRecord tagsPartRecord, string tagName) {

View File

@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Web.Mvc;
using System.Linq;
using Orchard.ContentManagement;
@@ -116,9 +117,9 @@ namespace Orchard.Widgets.Controllers {
return RedirectToAction("Index");
}
IEnumerable<LayerPart> layers = _widgetsService.GetLayers();
IEnumerable<LayerPart> layers = _widgetsService.GetLayers().ToList();
if (layers.Count() == 0) {
if (!layers.Any()) {
Services.Notifier.Error(T("Layer not found: {0}", layerId));
return RedirectToAction("Index");
}
@@ -147,8 +148,8 @@ namespace Orchard.Widgets.Controllers {
if (widgetPart == null)
return HttpNotFound();
try {
int widgetPosition = _widgetsService.GetWidgets().Where(widget => widget.Zone == widgetPart.Zone).Count() + 1;
widgetPart.Position = widgetPosition.ToString();
int widgetPosition = _widgetsService.GetWidgets().Count(widget => widget.Zone == widgetPart.Zone) + 1;
widgetPart.Position = widgetPosition.ToString(CultureInfo.InvariantCulture);
widgetPart.Zone = zone;
widgetPart.LayerPart = _widgetsService.GetLayer(layerId);
dynamic model = Services.ContentManager.BuildEditor(widgetPart);