From e732861961feca370d6472b5bd64242faaa2fab6 Mon Sep 17 00:00:00 2001 From: Dan McGee Date: Tue, 16 Feb 2010 19:41:16 -0600 Subject: Make packages view much more efficient The len() call was killing performance, especially in the default click-thru case where we show the packages list without any filtering (only sorting). Also use a well-placed select_related() to further drop the query count. As noted by the TODO, there is still more that can be done here to improve performance. Signed-off-by: Dan McGee --- packages/views.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/views.py b/packages/views.py index e828c263..487f20df 100644 --- a/packages/views.py +++ b/packages/views.py @@ -125,7 +125,7 @@ def __init__(self, *args, **kwargs): def search(request, page=None): current_query = '?' limit=50 - packages = Package.objects.all() + packages = Package.objects.select_related('arch', 'repo', 'maintainer') if request.GET: # urlencode can't handle unicode. One fix for this is to call: @@ -168,12 +168,13 @@ def search(request, page=None): page_dict = {'search_form': form, 'current_query': current_query } - if len(packages) == 1: + if packages.count() == 1: return HttpResponseRedirect(packages[0].get_absolute_url()) allowed_sort = ["arch", "repo", "pkgname", "maintainer", "last_update"] allowed_sort += ["-" + s for s in allowed_sort] sort = request.GET.get('sort', None) + # TODO: sorting by multiple fields makes using a DB index much harder if sort in allowed_sort: packages = packages.order_by( request.GET['sort'], 'repo', 'arch', 'pkgname') -- cgit v1.2.3-54-g00ecf