Ashwin Bande
Ashwin Bande

Reputation: 3053

Is their any time advantage of using @property decorator on django models instead of saving the field directly into database

So a field that can be computed like full_name from first and last name, we should use the @property to compute the full_name. But when we required to get a list of all 'n' persons with their full name. the full_name will be computed 'n' times which should require more time than just getting the field from database(if it stored as separate field already!).

So is their any processing time / db fetching time advantage /disadvantage of using @property to compute full_name?

(Note: I have considered other advantages of @property like reduction of database size, not worrying about change in first or last name without change in full name, setter function to set first and last name etc. I just want to know the processing/ db fetching time advantage/disadvantage over saving full_name into database.

Upvotes: 0

Views: 404

Answers (1)

Satevg
Satevg

Reputation: 1601

Technique that you're talking about is called Denormalization. This is quite advanced technique.

Denormalization is a strategy used on a previously-normalized database to increase performance. In computing, denormalization is the process of trying to improve the read performance of a database, at the expense of losing some write performance, by adding redundant copies of data or by grouping data.

It's opposite to database Normalization. And you always should start your application with normalized database.

If you don't have any serious problems with performance, I'd advise to not do this. If you have problems, try other solutions first to improve your app speed.

First Normal Form(1NF):

  • It should only have single(atomic) valued attributes/columns.

Very basic example of disadvantage: UPDATE statement. You'll need to access 2 columns in table + calculation for full_name.

Anyway, your full_name example is so simple, that you should definitely do this with @property


More on this topic: Difference Between Normalization and Denormalization

Upvotes: 1

Related Questions