简体   繁体   English

用户活动数据库结构

[英]user activity database structure

I am working on a community website. 我正在社区网站上工作。 I want to show the user's activity in 2 places in the website. 我想在网站的2个位置显示用户的活动。

  1. The User "A" Profile. 用户“A”配置文件。
  2. The Friends page of the user "A" friends. 用户“A”朋友的好友页面。 "What are your friends doing?" “你的朋友在做什么?”

The tables for instance are: 例如,表格是:

  • members 会员
  • members_gallery members_gallery
  • members_videos members_videos
  • members_friends members_friends

my problem is in the Sql structure. 我的问题是在Sql结构中。 I've read this question "User recent activities - PHP MySql" 我已经读过这个问题“用户最近的活动 - PHP MySql”

The "union" idea is good but I have an alternative one. “联盟”的想法很好但我有另一个想法。 I am going to make a new table called 我打算做个新桌子

  • members_activity members_activity

The fields: 田野:

id | user_id | photo | video | friend | p_id | v_id | f_id | datetime

let's say that the user has just uploaded an image . 假设用户刚上传了一张图片

id | user_id | photo | video | friend | p_id | v_id | f_id | datetime
1  |   15    |   1   |   0   |    0   | 1203 |   0  |   0  |  NOW()

advantages: 好处:

  • When i make a SELECT QUERY, i can easily know if it's a photo, video, or a friendship activity. 当我进行SELECT QUERY时,我可以很容易地知道它是照片,视频还是友谊活动。
  • The user can delete the 'photo activity' but keep the photo. 用户可以删除“照片活动”但保留照片。
  • Can notify friends of the user easily. 可以轻松通知用户的朋友。

disadvantages: 缺点:

  • Huge number of table rows? 大量的表行?

Any ideas, or suggestions how the big websites deal with it? 任何想法,或建议大型网站如何处理它? digg, facebook, etc. digg,facebook等

I think you're correct that a single-table approach is best here. 我认为你的单表方法最好是正确的。 One disadvantage, however, is that it doesn't scale well--what if you want to add link or comment activity types? 然而,一个缺点是它不能很好地扩展 - 如果你想添加链接或评论活动类型怎么办? With this model you'd have to add another column for each of those. 使用此模型,您必须为每个模型添加另一列。 An approach that I've seen in Rails-land is to use a polymorphic model, which would look like this: 我在Rails-land中看到的一种方法是使用多态模型,它看起来像这样:

id | user_id | activity_type_id | p_id | v_id | f_id | datetime

You can see that I've replaced video, photo, etc. with activity_type_id . 您可以看到我已使用activity_type_id替换了视频,照片等。 Then there would be a second table called activity_types : 然后会有第二个名为activity_types表:

 id | name
----+-------
  1 | photo
  2 | video
  3 | ...

Then when you create a members_activity record you can assign the appropriate activity_type_id , and if you want to create new activity types later on it's relatively painless, and you could SELECT a particular kind of activity with a simple JOIN , eg: 然后,当您创建members_activity记录时,您可以分配相应的activity_type_id ,如果您想稍后创建新的活动类型,则相对轻松,您可以使用简单的JOIN SELECT特定类型的活动,例如:

SELECT * FROM members_activity
  JOIN activity_types ON members_activity.activity_type_id = activity_types.id
 WHERE activity_types.name = 'photo';

If you have a huge number of rows, it is really not going to be a practical disadvantage as long as you index the table properly. 如果你有大量的行,只要你正确地索引表,它实际上不会是一个实际的缺点。

At the very least I would index user_id and datetime , assuming you will be selecting activity for a particular user and ordering by date. 假设您将为特定用户选择活动并按日期排序,我至少会将user_iddatetime索引。

Use MySQL's EXPLAIN (<query>) to ensure your indexes are optimised for the queries you are running often. 使用MySQL的EXPLAIN (<query>)来确保您的索引针对您经常运行的查询进行了优化。

  • I don't see why you need the friends id. 我不明白为什么你需要朋友的ID。 For the friends page you would first do a select for all his friends, then select from activity table where user_id in (2,6,89 etc) 对于朋友页面,你首先要为他的所有朋友做一个选择,然后从活动表中选择user_id in(2,6,89等)

  • I would make photo and video fields a single field called type, where photo and video would be values, this way you can later on add more activity types 我会将照片和视频字段设为一个名为type的字段,其中照片和视频将是值,这样您以后可以添加更多活动类型

  • I would make p_id and v_id a single column called item_id .. no need for 2 columns 我会使p_id和v_id成为名为item_id的单个列。不需要2列

  • I would add an extra column called info where I would store other information in a json format. 我会添加一个名为info的额外列,我将以json格式存储其他信息。 This is for the extra data that not all events have. 这是针对并非所有事件都具有的额外数据。 For example, you could have an event for adding a link to the profile ... and you could place the link there, since other events don't have urls and adding a column just for this event type would be not a good solution 例如,您可能有一个事件用于添加到配置文件的链接...并且您可以将链接放在那里,因为其他事件没有URL并且仅为此事件类型添加列将不是一个好的解决方案

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

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