Django查询很慢,优化

时间:2015-05-22 20:16:33

标签: jquery mysql sql django optimization

def search(request):
    found_entries = None
    query = Q(feedstock__fs_name__icontains=corn
    found_entries = Sample.objects.filter(query).exclude(status_id=3).order_by('id')
    queries = request.GET.copy()
    sample = paging(request, found_entries)
    return render_to_response('search_results.html', {'sample': sample, 'queries': queries},
                          context_instance=RequestContext(request))

这将返回大约9000个条目,查询需要很长时间。对于分页,如果单击下一页,则需要同样长的时间,就好像它重新查询数据一样。有关如何加快查询或优化查询的任何想法?

1 个答案:

答案 0 :(得分:0)

您是否尝试过使用Django分页来设置结果大小?如果不是,我要说看看https://docs.djangoproject.com/en/1.8/topics/pagination/

这是与你有关的Doc的长短:

来自django.core.paginator import Paginator,EmptyPage,PageNotAnInteger

def listing(request):
    contact_list = Contacts.objects.all()
    paginator = Paginator(contact_list, 25) # Show 25 contacts per page

    page = request.GET.get('page')
    try:
        contacts = paginator.page(page)
    except PageNotAnInteger:
        # If page is not an integer, deliver first page.
        contacts = paginator.page(1)
    except EmptyPage:
        # If page is out of range (e.g. 9999), deliver last page of results.
        contacts = paginator.page(paginator.num_pages)

    return render_to_response('list.html', {"contacts": contacts})