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) list.AddRange(_blogService.Get(VersionOptions.Latest)
.Select(b => { .Select(b => {
var blog = Services.ContentManager.BuildDisplay(b, "SummaryAdmin"); var blog = Services.ContentManager.BuildDisplay(b, "SummaryAdmin");
blog.TotalPostCount = _blogPostService.Get(b, VersionOptions.Latest).Count(); blog.TotalPostCount = _blogPostService.PostCount(b, VersionOptions.Latest);
return blog; return blog;
})); }));

View File

@@ -32,7 +32,7 @@ namespace Orchard.Blogs.Handlers {
var posts = blogPostService.Get(blogPostPart.BlogPart, VersionOptions.Published); 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 // 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) { foreach (var post in posts) {
if (!post.Has<CommonPart>()) if (!post.Has<CommonPart>())
continue; continue;

View File

@@ -43,7 +43,7 @@ namespace Orchard.Blogs.Handlers {
// Ensure the "right" set of published posts for the blog is obtained // Ensure the "right" set of published posts for the blog is obtained
blogPart.ContentItem.ContentManager.Flush(); 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(); options = new CommentIndexOptions();
// Filtering // Filtering
IContentQuery<CommentPart, CommentPartRecord> comments; IContentQuery<CommentPart, CommentPartRecord> commentsQuery;
switch (options.Filter) { switch (options.Filter) {
case CommentIndexFilter.All: case CommentIndexFilter.All:
comments = _commentService.GetComments(); commentsQuery = _commentService.GetComments();
break; break;
case CommentIndexFilter.Approved: case CommentIndexFilter.Approved:
comments = _commentService.GetComments(CommentStatus.Approved); commentsQuery = _commentService.GetComments(CommentStatus.Approved);
break; break;
case CommentIndexFilter.Pending: case CommentIndexFilter.Pending:
comments = _commentService.GetComments(CommentStatus.Pending); commentsQuery = _commentService.GetComments(CommentStatus.Pending);
break; break;
case CommentIndexFilter.Spam: case CommentIndexFilter.Spam:
comments = _commentService.GetComments(CommentStatus.Spam); commentsQuery = _commentService.GetComments(CommentStatus.Spam);
break; break;
default: default:
throw new ArgumentOutOfRangeException(); throw new ArgumentOutOfRangeException();
} }
var pagerShape = Shape.Pager(pager).TotalItemCount(comments.Count()); var pagerShape = Shape.Pager(pager).TotalItemCount(commentsQuery.Count());
var entries = comments var entries = commentsQuery
.OrderByDescending<CommentPartRecord, DateTime?>(cpr => cpr.CommentDateUtc) .OrderByDescending<CommentPartRecord, DateTime?>(cpr => cpr.CommentDateUtc)
.Slice(pager.GetStartIndex(), pager.PageSize) .Slice(pager.GetStartIndex(), pager.PageSize)
.ToList() .ToList()

View File

@@ -142,7 +142,10 @@ namespace Orchard.Tags.Services {
} }
public int GetTaggedContentItemCount(int tagId, VersionOptions options) { 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) { private void TagContentItem(TagsPartRecord tagsPartRecord, string tagName) {

View File

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