【发布时间】:2015-03-23 18:00:09
【问题描述】:
我目前正在做一个项目,我想在其中保存用户的统计信息。让我们假设,我正在保存他的 Facebook 页面的统计信息。 (页面喜欢,当天所有帖子的评论计数,当天所有帖子的分享计数。
问题是,我想保存计数,但还将数据作为历史记录保存在一个单独的表中。我还想要这些值的min/max 值。
现在我遇到的问题如下。我将在哪里保存当天的数据,我将在哪里保存最大/最小值?我会将包括max/min/current 值在内的最新数据保存到表facebook_pages 中并保存到facebook_history 中吗?
现在我有几个场景
场景 1:将最新数据保存在一个表中
//Table 1: facebook_pages
id
facebook_page_id
page_likes
page_likes_MAX
page_likes_MIN
post_likes
post_likes_MAX
post_likes_MIN
post_shares
post_shares_MAX
post_shares_MIN
post_comments
post_comments_MAX
post_comments_MIN
//Table 2: facebook_history
id
facebook_pages_id (referencing the id on the table facebook_pages)
page_likes
post_likes
post_shares
post_comments
checkDate (date when the data got gathered)
在这里,它会很简单。当我想要一个用户的所有数据时,我只需要运行一个查询
"SELECT * FROM facebook_pages WHERE id = {ID}";
场景 2:仅将最大值/最小值保存到一个表中
//Table 1: facebook_pages
id
facebook_page_id
page_likes_MAX
page_likes_MIN
post_likes_MAX
post_likes_MIN
post_shares_MAX
post_shares_MIN
post_comments_MAX
post_comments_MIN
//Table 2: facebook_history
id
facebook_pages_id (referencing the id on the table facebook_pages)
page_likes
post_likes
post_shares
post_comments
checkDate (date when the data got gathered)
这个也很简单(注意,这里只是伪代码)
"SELECT * FROM facebook_pages INNER JOIN facebook_history
WHERE id = {ID} AND checkDate = {TODAY}";
场景 3:仅保存历史记录并在需要数据时查询 Max/Min
//Table 1: facebook_pages
id
facebook_page_id
page_likes
post_likes
post_shares
post_comments
//Table 2: facebook_history
id
facebook_pages_id (referencing the id on the table facebook_pages)
page_likes
post_likes
post_shares
post_comments
checkDate (date when the data got gathered)
这里的这个也很简单。只需查询ID,然后使用MySQL的MAX和MIN函数进行选择。
如您所见,上述所有场景都很简单。我只是在想,哪一个是最好的?
从数据库工程师的角度来看?从逻辑的角度看?哪种情况最有意义?
【问题讨论】:
标签: mysql sql database database-design