⬅︎ Back to In Django, how much faster is it to aggregate?
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 += valueor 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 ?
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.
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 ?
Replies
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.