I have this part of the code in my API which recently has become a somewhat bottleneck:
total = results.count()
if request.GET.has_key('offset'):
offset = int(request.GET.get('offset').strip())
results = results.order_by('name')[100*offset:100*(offset+1)]
people = list(results)
Note that results
is the queryset of all the people and offset
is a param used for pagination.
Here I can see, when I print connection.queries
, that my database getting hit twice by .count()
and list(results)
. The reason why .count()
has to be at the top because I need to the length of all the people (Not 100.) Is there a way to get around this?