存储用户活动? PHP,MySQL和数据库设计

时间:2011-12-02 22:12:17

标签: php mysql sql database database-design

好的,所以用户来到我的网络应用程序并获取活动的积分等,与此网站类似(但不是那么复杂)。他们可以投票,评论,提交,收藏,投票评论,写作描述等等。

目前,我将用户操作存储在表格中,而不是像这样的日期

Table user_actions
    action_id       - PK AI int
    user_id         - PK int
    action_type     - varchar(20)
    date_of_action  - datetime

因此,例如,如果用户出现并对评论留下评论或投票,那么行将看起来像这样

    action_id       = 4
    user_id         = 25
    action_type     = 'new_comment'
    date_of_action  = '2011-11-21 14:12:12';

    action_id       = 4
    user_id         = 25
    action_type     = 'user_comment_vote'
    date_of_action  = '2011-12-01 14:12:12';

我听到你说的一切都很好,但不完全记住,请记住这些行将驻留在user_actions表中,该表与存储注释和用户评论投票的表不同。

那么我怎么知道什么评论链接到user_actions中的哪一行?

我可以将评论表中的唯一comment_id链接到target_primary_key表格中名为user_actions的新列吗?

不。不能这样做,因为动作可能同样是user_comment_vote,它有一个复合键(双键)?

所以我留下的想法是,我只是在列中添加主键并用逗号去除它们并让PHP解析它吗?


以上为例,下面的行显示了如何存储目标主键

new_comment
target_primary_keys - 12 // the unique comment_id from the comments table 

user_comment_vote
target_primary_keys - 22,12 // the unique comment_id from the comments table 


所以基本上用户做了一个动作,user_actions被更新了,特定的table也是如此,但是如何在仍然允许多个密钥的同时将这两个链接起来?

有没有人有过存储用户活动的经验?

欢迎任何想法,这里没有错误的答案。

2 个答案:

答案 0 :(得分:2)

您不需要用户操作表。 要计算“得分”,您可以在多个表上运行一个查询,并将匹配的评论,评级等的数量乘以乘数(评论为25分,评分为10,......)。

要加快页面速度,您可以将总分存储在额外的表格或用户表格中,并在分数发生变化时使用触发器刷新总分数。

如果您想显示评分或评论的数量,您可以这样做。

从现有表格中获取详细信息,并将评论和评分的总数存储在一个额外的表格中。

答案 1 :(得分:0)

最简单的答案是只使用另一个表,它可以包含任何键的多个匹配项,并允许很好的索引选项:

create table users_to_actions (
   user_id int(20) not null,
   action_id int(20) not null,
   action_type varchar(25) not null,
   category_or_other_criteria ...
);

create index(uta_u_a) on users_to_actions(user_id, action_id);

要稍微扩展一下,您可以通过将它们与此表连接来选择项目:

select
  *
from
  users_to_actions as uta join comments as c using(action_id)
where
  uta.action_type = 'comment' and user_id = 25
order by
  c.post_date

或者可能是嵌套查询,具体取决于您的需求:

  select * from users where user_id in(
      select
         user_id
      from 
         users_to_actions
      where
         uta.action_type = 'comment'
  );
相关问题