[Django]-Should I reverse order a queryset before slicing the first N records, or count it to slice the last N records?

5👍

This one is going to be very slow

 data = MyModel.objects.filter(criteria=something)[index-50:]

Why because it translates into

 SELECT * FROM myapp_mymodel OFFEST (index-50)

You are not enforcing any ordering here, so the server is going to have to calulcate the result set and jump to the end of it and that’s going to involve a lot of reading and will be very slow. Let us not forgot that count() queries aren’t all that hot either.

OTH, this one is going to be fast

data = MyModel.objects.filter(criteria=something).order_by('-pk')[:50]

You are reverse ordering on the primary key and getting the first 50. And the first 50 you can fetch equally quickly with

data = MyModel.objects.filter(criteria=something).order_by('pk')[:50]

So this is what you really should be doing

data1 = MyModel.objects.filter(criteria=something).order_by('-pk')[:50]
data2 = MyModel.objects.filter(criteria=something).order_by('pk')[:50]

The cost of ordering on the primary key is very low.

👤e4c5

Leave a comment