Lucas Ou-Yang
Lucas Ou-Yang

Reputation: 5655

Reducing memory consumption in django models, extra fields versus extra operations

I wanted to find out which of these two strategies was the preferred one among most django devs.

I have a model, there is a field i'd like to order these models by called "hotness". Hotness is computed from already existing fields in this model.

So the two paths are:

 1) Have a field called "hotness" which the models 
    can be easily ordered by.
 2) Have a method called "get_hotness()", which 
    returns the hotness of the instance.

But there are pro's and con's to both.

If option one were to be used, the extra field would result in extra memory consumption, as the models would be heavier themselves.

For option two, there actually isn't a way to filter a queryset by a models method in django, so we would probably have to do operations on a list:

models = sorted(list(Model.objects.all()), key = lamba x: x.get_hotness())

But using list operations once again would increase memory consumption versus just a query.

So if anyone out there can help with what is the preferred route for something like this, advice would be awesome! Thank you.

Upvotes: 0

Views: 98

Answers (1)

Qiang Jin
Qiang Jin

Reputation: 4467

You need to consider both the data size and the changing requirement.

If the hotness definition remains in a certain period, I will chose to have a hotness field, since it ease the selection part.

If the definition of hotness is easy to change, I prefer to have get_hotness() method, since it doesn't required to update pre-calculated hotness data in database.

And the data size is another concern, if you need sort the entire elements by hotness, the may easily exceed the memory limit when loading all data into memory with Models.object.all().

Upvotes: 1

Related Questions