简体   繁体   中英

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.

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

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.

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.

Option 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.

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.

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. So my table looks like:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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