Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Pagination too slow with large dataset

I have a problem regarding Django's Pagination. When I try to paginate over 200k records the portal Web load very slow (>10 secs), and I'm suppose to use around 2 million records.

I can't find on Stackoverflow or other site a GOOD specific fix to this problem. Everytime the code gets a page it executes the QuerySet, which goes over a very large dataset, making it slow.

Anybody knows what can be done? I've searched everywhere and couldn't fix this issue. Below I post the code to paginate. Paper_list is: model.object.all().filter(category=x) (and there are around 200k objects now (all of them belong to that category too).

def paginate_context(paper_list, request, context, source, author, title, term, date):
num_papers = len(paper_list) 
paginator = Paginator(paper_list, 4) # Show X papers per page
page = request.GET.get('page')
try:
    papers = paginator.page(page)
except PageNotAnInteger:
    # If page is not an integer, deliver first page.
    papers = paginator.page(1)
except EmptyPage:
    # If page is out of range (e.g. 9999), deliver last page of results.
    papers = paginator.page(paginator.num_pages)      
context['papers'] = papers
context['num_papers'] = num_papers
context['query_cat'] = create_request_str_and(context['cat'], source, author, title, term, date)
like image 643
E.Diaz Avatar asked Jan 12 '16 09:01

E.Diaz


People also ask

Does pagination improve performance Django?

Additionally, switching to keyset pagination will improve the performance of page lookups and make them work in constant time. Django makes it easy to alter its default configuration, giving you the power to build a performant solution for pagination in Django.

What is Paginator in Django?

Django provides a few classes that help you manage paginated data – that is, data that's split across several pages, with “Previous/Next” links. These classes live in django/core/paginator.py. For examples, see the Pagination topic guide.

What does it mean to paginate a document?

Pagination is the process of separating print or digital content into discrete pages. For print documents and some online content, pagination also refers to the automated process of adding consecutive numbers to identify the sequential order of pages.


1 Answers

As I can see in the code, num_papers = len(paper_list) method evaluates the query, hence could hamper performance. You can change it to:

num_papers = paper_list.count()

You can check here about when the queryset is evaluated: https://docs.djangoproject.com/en/1.9/ref/models/querysets/#when-querysets-are-evaluated

like image 56
ruddra Avatar answered Sep 24 '22 08:09

ruddra