Answer the question
In order to leave comments, you need to log in
How to speed up pagination in Django?
There are ~3.5 million records in the table, but the server is dead.
I'm using the standard Paginator from Junga.
I understand that he first makes a COUNT(*) ... query, and then the already passed QuerySet.
At first everything was ok, but when the QuerySet became more complicated (an annotation was added), the COUNT(*)... query from the paginator began to slow down wildly (> 10sec). The QuerySet itself is executed instantly (by index). Those. the problem is in the COUNT(*)... query of the paginator, where there is even ...GROUP BY... by the primary key - in short, it doesn't fit at all.
An option is floating in my head:
Do offset / limit yourself, but information about the number is not needed. Those. the end of pagination is not known in advance. And to find out that this was the last page - do limit by 1 more than the page size.
However, as a beginner, you would like to know how such problems are usually solved? And are there any ready-made solutions (maybe even in jang itself). I can bike, but it would be stupid if there is a (de facto) standard for such situations.
Answer the question
In order to leave comments, you need to log in
An error has occurred.
Initially, in order to optimize, I decided to limit the maximum offset, because the user does not need to scroll deeply and it is much more reasonable to play with the search filter. In my opinion, a common technique (for example, habr).
Since the django paginator has no way to limit the offset, the following is done:
qs = MyModel.objects.filter(...)
objects = qs[:MAX_PAGE * PER_PAGE] #2
paginator = Paginator(objects, PER_PAGE)
page = paginator.page(...)
qs = MyModel.objects.filter(...)
objects = list(qs[:MAX_PAGE * PER_PAGE]) #2 !!!
paginator = Paginator(objects, PER_PAGE)
page = paginator.page(...)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question