I'm trying to make a reddit-like web application from scratch. I'm not sure how to store the up and down votes.

我正试图从头开始制作类似reddit的Web应用程序。我不确定如何存储上下投票。

I'm thinking about creating a table called 'user_votes' with fields 'id', 'user_id', 'voted_link_id', 'up_or_down'

我正在考虑创建一个名为'user_votes'的表,其中包含字段'id','user_id','voted_link_id','up_or_down'

So it's basically adding a row for "who voted what on what" every time a user votes.

因此,每次用户投票时,它基本上都会为“谁投票什么”添加一行。

I'm inserting a new row instead of just adding 1, because the user profile page will have to show the list of links the user voted. So I need to keep track of every single votes. But I don't feel like it's efficient.

我正在插入一个新行,而不是只添加1,因为用户个人资料页面必须显示用户投票的链接列表。所以我需要跟踪每一张选票。但我觉得它没有效率。

I'm not familiar with web applications heavily relying on DB. Please guide me.

我不太熟悉严重依赖数据库的Web应用程序。请指导我。

P.S. Which columns should be indexed?

附:哪些列应编入索引?

3 个解决方案

#1


3

Actually you want both.

其实你想要两个。

You must have the table with Article_ID, User_ID, +1 or -1. This is for exactly the reasons you've stated. You'll need to show a user's votes as a running account. You'll also us that to ensure uniqueness.

您必须具有Article_ID,User_ID,+ 1或-1的表。这正是您所说的理由。您需要将用户的投票显示为正在运行的帐户。你也会帮我们确保独特性。

You now have to think about frequency. Articles will be viewed more frequently than voted on. Because of that, you'd have to do a lot of SUMs on the Vote table that result in the exact same values being produced.

你现在必须考虑频率。文章将比投票更频繁地被查看。因此,您必须在Vote表上执行大量SUM,这会导致生成完全相同的值。

Instead you should keep two counters on the Article table: a total of upvotes and a total of downvotes. The reason for two is that, the sum is inconsequential since the two values are on the same row. Second you may want to implement something that will expose those values like SO does. (You can't see that until you get so many points (about 1500)).

相反,你应该在Article表上保留两个计数器:总共upvotes和总共downvotes。两个原因是,总和是无关紧要的,因为这两个值在同一行上。其次,您可能希望实现能够像SO那样公开这些值的东西。 (直到你得到这么多积分(大约1500)你才能看到它)。

You may want to show a list of articles and the total points each has... you don't want to SUM over the votes table for a long list of articles. You also may want to allow people to set limits on articles, "only show me over +10". Again, you don't want to sum over the votes table every time someone opens their home page.

您可能希望显示文章列表以及每个文章的总分数...您不希望在投票表中查找一长串文章。您也可能希望允许人们对文章设置限制,“只显示超过+10”。同样,每当有人打开他们的主页时,你不想总结投票表。

更多相关文章

  1. 为mysql数据备份建立最小权限的用户
  2. MySQL查询返回用户的收件人列表
  3. 根据用户时区显示当地时间 php+javascript
  4. Google Maps API v3:如何设置缩放级别和地图中心到用户提交的位置
  5. Ajax的实用技术——用户的注意力从页面的其他区域到转移选择的图
  6. 阻止用户在表单字段中输入
  7. 将(重度公式加载的)Excel电子表格转换为用户友好的网络计算器
  8. 用户打开一个DropDown关闭其他DropDowns
  9. 在用户将'n'粘贴复制到文本字段后,如何更新视图模型?

随机推荐

  1. 字典树概念与题型解析
  2. 程序员的快乐往往就是这么朴素无华且枯燥
  3. 架构 | 到底该不该使用JavaScript框架
  4. 2017年 Web 开发趋势
  5. 超详细!详解一道高频算法题:数组中的第 K
  6. 软件设计原则
  7. 短小精悍,双指针对撞,求解「两数之和 II」
  8. 从零到一学懂区块链(6):哈希函数
  9. (再进阶版)有了四步解题法模板,再也不害怕动
  10. JavaScript 数据结构(2-2):栈与队列-队列篇