I’m wondering what the best practice is for having redundant columns in a MySQL database. I’m wondering this because I have a User table in my database but I also want to be able to display many statistics about each individual user. These stats might include how many times a user has interacted with another model or how many messages they have.
Does it hurt performance to search for all records of a certain object (such as messages) in order to count them? Or is it better to make a redundant column in the user table called total_messages that gets updated every time a message is added/removed? Thanks, this has been confusing me for awhile.
The short answer: do both
As a rule of thumb, I don’t like creating and updating data that can be calculated on the fly (i.e. counting). Therefore, I would hold off on adding those redundant statistics until you find performance bottlenecks and adjust accordingly.
You can always add retroactive statistics based on user activity, so you could “grow” your application by only adding the redundancy you absolutely need.
Adding redundant data can improve your performance, but it is never a one size fits all solution and you could end up hurting overall performance if you are constantly trying to calculate and update usage values that can be calculated on the fly with a simple request.