简体   繁体   English

针对特定于用户的活动提要的最佳MySQL设计

[英]Optimal MySQL design for user-specific activity feeds

I'm building a website that constructs both site-wide and user-specific activity feeds. 我正在建立一个网站,该网站可以构建网站范围和特定于用户的活动供稿。 I hope that you can see the structure below and share you insight as to whether my solution is doing the job. 希望您能看到下面的结构,并分享您对我的解决方案是否正在完成工作的见解。 This is complicated by the fact that I have multiple types of users that right now are not stored in one master table. 我有多种类型的用户现在不存储在一个主表中,这使情况变得复杂。 This is because the types of users are quite different and constructing multiple different tables for user meta-data would I think be too much trouble. 这是因为用户的类型完全不同,为用户元数据构造多个不同的表会给我带来太多麻烦。 In addition, there are multiple types of content that can be acted upon, and multiple types of activity (following, submitting, commenting, etc.). 此外,可以采取多种类型的内容,并进行多种类型的活动(跟踪,提交,评论等)。

Constructing a site-wide activity feed is simple because everything is logged to the main feed table and I just build out a list. 构建一个站点范围的活动提要很简单,因为所有内容都记录到了主提要表中,而我只是建立了一个列表。 I have a master feed table in MySQL that simple logs: 我在MySQL中有一个主供稿表,其中包含简单的日志:

  1. type of activity; 活动类型;
  2. type of target entity; 目标实体的类型;
  3. id of target entity; 目标实体的ID;
  4. type of source entity (ie, user or organization); 源实体的类型(即用户或组织);
  5. id of source entity. 源实体的ID。

(This is just a big reference table that points the script generating the feed to the appropriate table(s) for each feed entry). (这只是一个很大的参考表,该表将生成提要的脚本指向每个提要条目的适当表)。

In generating the user-specific feed, I'm trying to figure out some way to join the relationship table with the feed table, and using that to parse results. 在生成特定于用户的提要时,我试图找出某种方法将关系表与提要表结合在一起,并使用该方法来解析结果。 I have a relationships table, comprised of 'following' relationships, that is similar to the feed table. 我有一个关系表,由“以下”关系组成,类似于供稿表。 It is simpler though b/c only one type of user is allowed to follow other content types/users. 尽管b / c仅允许一种类型的用户关注其他内容类型/用户,这更简单。

  1. user/source id; 用户/源ID;
  2. type of target entity; 目标实体的类型;
  3. id of target entity. 目标实体的ID。

Columns 2 & 3 in the feed and follow table are the same, and I have been trying to use various JOIN methodologies to match them up, and then limit them by any relationships in the follow table that the user has. 提要和关注表中的第2列和第3列是相同的,我一直在尝试使用各种JOIN方法进行匹配,然后通过用户在关注表中的任何关系来限制它们。 This is has not been very successful. 这不是很成功。

The basic query I am using is: 我正在使用的基本查询是:

 SELECT *
 FROM (`feed` as fe) LEFT OUTER JOIN `follow` as fo
                     ON `fe`.`feed_target_type` = `fo`.`follow_e_type`
                        AND fo.follow_e_id = fe.feed_target_id
 WHERE `fo`.`follow_u_id` = 1 OR fe.feed_e_id = 1
       AND fe.feed_e_type = 'user'
 ORDER BY `fe`.`feed_timestamp` desc LIMIT 10

This query also attempts to grab any content that the user has created (which data is logged in the feed table) that the user is, in effect, following by default. 该查询还尝试获取用户创建的任何内容(默认情况下,实际上是用户创建的内容(数据记录在提要表中)。

This query seems to work, but it took me sometime to get to it and am pretty sure I'm missing a more elegant solution. 该查询似乎有效,但是花了我一些时间才能确定,但​​是我肯定我错过了一个更优雅的解决方案。 Any ideas? 有任何想法吗?

The first site I made with an activity feed had a notifications table where activities were logged, and then friends actions were pulled from that. 我使用活动供稿创建的第一个站点有一个通知表,其中记录了活动,然后从中提取了朋友的动作。 However a few months down the line this hit millions of records. 但是几个月后,这创下了数百万条记录。

The solution I am programming now pulls latest "friends" activities from separate tables and then orders by date. 我正在编程的解决方案现在从单独的表中提取最新的“朋友”活动,然后按日期排序。 The query is at home, can post the example later if interested? 查询在家里,如有兴趣可以稍后发布示例吗?

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

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