简体   繁体   English

如何在关系数据库中存储滚动周的展示次数?

[英]How to store a rolling week of impressions in a relational database?

I'm working on a leaderboard in my facebook app that shows how many times you have performed an action (opened a webpage) in the past week compared to your friends. 我正在我的Facebook应用程序的排行榜上工作,显示过去一周与朋友相比你执行过多少次操作(打开一个网页)。

I need some way to store this rolling weekly data in a relational database and would like some advice on the design of the database. 我需要一些方法将这个滚动的每周数据存储在关系数据库中,并希望对数据库的设计提出一些建议。

Option 1 选项1

Have a table to stores every action as a row with a user ID and a timestamp and then count query for all actions associated with each friend that have timestamp > today - 1 week. 有一个表将每个操作存储为一行,其中包含用户ID和时间戳,然后计算与每个朋友关联的所有操作的查询,这些操作具有时间戳>今天 - 1周。

This solution is easy to implement, but I'm worried about scale. 这个解决方案很容易实现,但我担心规模。 If I have 1000 facebook friends, I will have to do this sum query 1000 times over a dataset that potentially contains millions of actions. 如果我有1000个Facebook好友,我将不得不对可能包含数百万个动作的数据集进行1000次此总和查询。

Option 2 选项2

Have the user table contain a column for each day of the week containing the number of actions performed on that day. 让用户表包含一周中每天的列,其中包含当天执行的操作数。 Then I can sum over the day columns in order to get the total for the week. 然后我可以总结一天的列,以获得本周的总数。 Then, at the start of each day, the day's value will reset to 0. 然后,在每天的开始,日的值将重置为0。

This solution will scale nicely, but I'm stuck on how to implement logic that will tell my application whether to overwrite the entry for "today" or increment it. 这个解决方案可以很好地扩展,但我仍然坚持如何实现逻辑,告诉我的应用程序是否覆盖“今天”的条目或增加它。

Any suggestions for alternate designs or thoughts on these designs are welcome. 对于这些设计的替代设计或想法的任何建议都是受欢迎的。

The Option 2 is almost perfect. 选项2几乎是完美的。

Instead of day of the weak use day as a primary key. 而不是将弱日当天作为主要关键日。 Then increment the value of the day every time. 然后每次增加当天的值。

I ended up going with option 2 and used a "today is a new day" boolean flag for each day. 我最终选择了选项2并且每天使用“今天是新的一天”布尔标志。 So my table looks like: 所以我的表看起来像:

id   | monday | monday_new | tuesday | tuesday_new | ... | sunday | sunday_new | lifetime
INT  | INT    | BOOL       | INT     | BOOL        | ... | INT    | BOOL       | INT

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM