0

I'm new to web development and databases (I am currently using Django and PostgreSQL) and I have a general question about databases, because other than the fact that they store data, I pretty much know nothing about them.

I was wondering if it's good practice to have as few fields as possible in my models?

For instance, I have a Model that has a DateTimeField() and was considering also creating a CharField() that corresponds to the month that the instance is related to (I won't delve into details). The consideration of adding this month field sparked me to ask this question.

I clearly don't need the month field because I could parse it out of the DateTimeField(), but it's more convenient to just have a string with the month name rather than parse it. Is it acceptable to add another field for convenience or should I have as few fields as possible?

rigdonmr
  • 2,662
  • 3
  • 26
  • 40

3 Answers3

1

Really not very pleasant to have to much field, but if a field is month like that, you can use computed field. I don't know if that exists in Django. If not, just use a view to show that month. You will get trouble when updating that and forgot to update one of the field holding same information.

ainasiart
  • 382
  • 2
  • 10
0

My experience: if database is stored on fast hard disk, best much more fields, because you save time to write code and cpu resources of machine where is the client of database; instead of databse is on old or not fast machine, is better that the client make the job. Same if the connection from client and server is slow.

0

What you want to do is called Database Denormalization. By doing that you can optimize the read performance of the table, but you also have to take responsibility that any redundant copies are kept consistent.

So do you really have such performance drawback? (I doub it but..) If you really have, what you can do before adding redundant columns is to try adding column indexes.

Community
  • 1
  • 1
Todor
  • 15,307
  • 5
  • 55
  • 62