Which one is better for voting function

Which system will be the best to vote and why ?;

  • Putting votes separately in the database table and calculating the average value, when necessary

  • It has only one row for one product. When a new vote arrives, receiving this database of votes, calculate it again and update the record. for example, we have a voice recording for product 1. His voice is totally 326 inside the database and 45 people voted. then we get a new vote, which is 4 for product 1. We take a record from the database, and then perform the following function:

(326 + 4) / (45 + 1)

then save it in the database with a total value of 330 and 46 as people voting.

I always offer the first option, but I want to know what others are doing.

+3
source share
3 answers

Well, it always depends on what you want, with additional voting information.

If you want to know, for example, trends in voting (average growth up or down), or you want to know that the votes are relatively old / new or that the user has voted (1 vote per person, check-person), etc. . etc. etc. You want to store each vote in a separate table.

If all you care about is the end result, the second option is more effective.

+3
source

If you want to suppress that people can vote twice, you still need an approach 1-row-per-vote, so I recommend the first option.

- - , .

+1

There are many factors to consider. Do you need to protect yourself from the one who attacks your application server and changes the number of votes? Are you worried that people's voices can be detected? Are you worried about blocking issues? All this will affect the decision, and the answers to them vary greatly depending on the specific scenario.

0
source

All Articles