Isn't that mainly the cost of creating model instances ? Can you try:
s = Decimal('0')
for value in some_queryset.values_list('cost', flat=True):
s += value
or even:
s = sum(qs.values_list('cost', flat=True), Decimal('0'))
Should be much faster then the first solution.
PS. Is there a way to indent code in comments ?
Comment
Ok. Benchmarked that too quickly. Using `values_list` is about 10 times slower than doing the aggregate in the database.
Faster than doing the full ORM but still not as fast as letting the database do it.
Parent comment
Isn't that mainly the cost of creating model instances ? Can you try: s = Decimal('0') for value in some_queryset.values_list('cost', flat=True): s += value or even: s = sum(qs.values_list('cost', flat=True), Decimal('0')) Should be much faster then the first solution. PS. Is there a way to indent code in comments ?