So this is not a post specific to Django, but the examples I give are.
The problem I was looking for a solution for was that I wanted to benchmark my Django ORM queries to find ways to improve performance. More specifically, I wanted to find the optimum way to count objects returned as querysets.
I stumbled across MAX() and COUNT() and wanted a raw speed performance comparison between the two. Turns out there is a way without leaving the python shell.
Here it is:
>>> from django.db.models import Max,Count >>> _t = time.time(); x = Article.objects.aggregate(Max('id')); "Took %ss"%(time.time() - _t ) 'Took 0.00190091133118s'
and using Count():
>>> _t = time.time(); x = Article.objects.aggregate(Count('id')); "Took %ss"%(time.time() - _t ) 'Took 1.34142112732s'
As you can see – quite a significant difference on ~108 000 rows. This is a good way to get a quick look at how a queryset call performs when there is more than one way in which it can be constructed.