投票功能哪一个更好
哪种系统最适合用于投票功能,为什么? ;
将投票单独放入数据库表中,并在需要时计算平均值
一种产品只有一行。当新的投票到来时,从数据库中获取该投票,再次计算并更新记录。例如,我们有一个产品1的投票记录。它的投票数在数据库中共有326个,有45人投票。然后我们收到新的投票,即产品 1 的 4 票。我们从数据库中获取记录,然后执行以下功能;
(326 + 4)/(45 + 1)
然后再次将其保存到数据库中,总价值为 330 和 46 作为投票者。
我总是选择第一个选项,但我想知道其他人在做什么。
Which system would be the best to go ahead with for voting function and why? ;
Putting the votes separately inside a database table and calculating the average when it is needed
Having only one row for one product. When a new vote comes, getting that vote form database, calculate it again and update the record. for example, we have a vote record for product 1. Its vote is totally 326 inside database and 45 people have voted. then we receive a new vote which is 4 and for product 1. We take the record from database and then do the following function;
(326 + 4) / (45 + 1)
then save it to database again with total value of 330 and 46 as voted people.
I always go ahead with the first option but I wanna know what others are doing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
嗯,这始终取决于您想要了解有关投票的附加信息。
例如,如果您想了解投票趋势(平均值是上升还是下降),或者您想知道投票相对较旧/较新,或者哪些用户投了票(每人 1 票检查) )等等等等,你想将每个投票存储在一个单独的表中。
如果您只关心最终结果,那么第二个选项会更有效。
Well, it always depends on what you want to with the additional information about the votes.
If you want to know for example trends in voting (is the average going up or down) or you want to know it the votes are relatively old / new or what user has casted the vote (the 1-vote-per-person-check) 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 way more efficient.
如果您想禁止人们投票两次,您无论如何都需要
1-row-per-vote
方法,所以我推荐第一个选项。第二种选择可能在性能方面更好 - 但如果这成为问题,使用缓存机制很容易。
If you want to suppress that people can vote twice you will need the
1-row-per-vote
approach anyway, so I recommend the first option.The second option might be better performance-wise - but should that ever be a problem, it's easy to use a caching-mechanism.
有很多因素需要考虑。您是否需要防御有人攻击您的应用程序服务器并修改投票计数?您是否担心人们的投票会被发现?您是否担心与锁定相关的性能问题?所有这些都会影响决策,并且根据具体情况,答案会有很大差异。
There are lots of factors to consider. Are you needing to defend against someone attacking your application server and modifying the vote count? Are you concerned with people's votes being discoverable? Are you concerned about locking-related performance issues? All of those will affect the decision, and the answers to those vary wildly depending on the specific scenario.