R
R
Roman Kitaev2015-07-07 13:41:27
Django
Roman Kitaev, 2015-07-07 13:41:27

Is aggregation more efficient?

Hello. There is code that calculates the rating of an entity in a very primitive way: the sum of all rating changes (-1 for "dislike", +1 for "like").
sum([vote.change for vote in self.votes.all()])
Would it be better (if so, how much?) to use aggregation instead?

self.votes.aggregate(Sum('change'))['change__sum'] or 0

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
sim3x, 2015-07-07
@deliro

Premature optimization?
It’s better to measure it yourself, but the second method should theoretically be faster, since memory will not be allocated in python under list, and it can be gigantic
# see what request is generated and what response you get

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question