数据库设计 - 为用户存储积分的方法
只是寻找一些有关如何为此进行数据库设计的建议。
在我的网站上,用户可以通过执行不同的活动获得积分。目前,我有 3 项奖励积分的活动 - 但设计必须具有可扩展性,我也可以添加其他奖励积分的活动。
所以今天——用户获得积分 1)当他添加一个新商店时,他获得10分(商店信息存储在STORE表中) 2)当他回答问题时,他得到 7 分(问题/答案存储在 ANSWERS 表中) 3) 当他推荐加入该网站的朋友时,他会得到 5 分
这就是我到目前为止所得到的 - 但看起来不太对:)
Points_Table 点_id 用户身份 行动(这将捕获所给予的点的行动) 。
我应该能够从数据库中推断出该用户因创建此商店或推荐这些朋友或回答此问题而获得了 xxxx 积分 上面的设计显然没有考虑到这一点。
感谢您的提示
Just looking for some suggestions on how to approach the database design for this.
On my site a user can get points for performing different activities. Currently there are 3 activities for which I award points - but the design has to be scalable where I can add other activities for awarding points as well.
So today - the user gets points
1) When he adds a new store he gets 10 points (Store information is stored in STORE table)
2) When he answers a question he gets 7 points (Questions/answers are stored in a ANSWERS table)
3) When he refers friends who join the site he gets 5 points
So this is what I have so far - but it doesnt look right :)
Points_Table
point_id
user_id
action (This will capture for what action the points are being given)
points
I should be able to deduce from the database that this user got xxxx points for creating this store or for referring these friends or for answering this question.
The above design obviously doesn't take care of that.
Thanks for your tips
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
根本不存储积分。只需对此视图进行查询即可。这样,它在面对任意变化时将具有鲁棒性。
编辑添加:
这遵循正常的数据库设计原则。所有数据库规范化都可以概括为:不要重复数据。
包含点表本质上只是重复数据库其余部分已经包含的信息。因此,应该省略它。当考虑维持数据一致性必须达到的长度时,这一点就很清楚了。不用担心积分,如果推荐归因于 Alice,但后来确定 Bob 应该获得积分,则这将需要更改单个表的单行中的单个字段。如果包含积分表,那么它也必须以某种方式更新。如果存储了积分摘要,那么愿上帝怜悯您的数据库。
可以使用视图或存储过程代替存储点,然后当数据变化时自动调整点系统。
我认为在这种情况下,视图比存储过程更好,因为可能需要通过多种方式分析点数据,这提供了最大的灵活性,并且为优化器提供了识别最佳路径的最佳机会。
您可以通过 user_id 或 action_type 从视图中进行选择,或者对点数列进行求和以获取某个用户或所有用户的总计。看看您可以享受的乐趣,而且它几乎是免费的!
Don't store the points at all. Just do queries on this view. That way it will be robust in the face of arbitrary changes.
Edited to add:
This follows the normal database design principles. All database normalization can be summed up with this: don't repeat data.
The inclusion of a points table essentially just repeats information that the rest of the database already contains. Therefore, it should be omitted. This is made clear when considering the lengths to which one must go to maintain the consistency of the data. Without worrying about the points, if a referral is attributed to Alice, but later it is determined that Bob should receive the credit, this will require a change to a single field in a single row of a single table. If a points table is included, then it must also be updated in some way. If points summaries are being stored, then may God have mercy on your database.
Rather than storing the points, a view or stored procedure may be used instead, and then the points system is adjusted automatically when the data changes.
I think a view is perferrable to a stored procedure in this case, because there are many ways in which the points data may need to be analyzed, and this provides the most flexibility, and it gives the optimizer the best chance at identifying the optimal path.
You may select from the view by user_id or by action_type or sum the points column to get totals for a user or for all users. Look at the fun you can have, and it's practically free!
添加一个表来存储用户获得积分的操作(列出指向不同表中实际操作的操作的通用表)。该表可能类似于 Reward_Actions(ActionId, ActionType, Points Earn) 等,您可以通过此添加不同类型的奖励操作。
Add a table that stores the actions (generic table listing actions that point to the actual action in different table) for which the user gets points. The table might be something like Reward_Actions(ActionId, ActionType, Points earned) etc. and you can add different types of rewarding actions through this.
典型的设计是使用 User 表、Action 表和 User_Action 表来记录用户执行的所有操作。您需要两个外键,可能还需要一个主键,以及完成该操作的日期戳。然后,您可以将每个操作的分数存储在操作表中,每当您查找总分数时,只需连接表并按时间戳排序,这样您就可以获得其分数的历史记录。
A typical design for this would be to have the User table, the Action table, and the User_Action table for recording all actions the user fulfills. You'd need the two foreign keys and probably a primary key in there, along with a date stamp for when they accomplished it. Then you could store the points for each action in the Action table, and whenever you do a lookup for total points, just join the the tables and order by the timestamp so you can get a history of their points.
如果您确实希望能够将该操作追溯到存储或答案表中的一行,请添加第四列,它是该操作添加的行的 pk 条目。
If you do want to be able to trace the action back to a row in the store or answers table, add a fourth column, which is the pk entry for the row that action added.