[英]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个位置显示用户的活动。
The tables for instance are: 例如,表格是:
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
我打算做个新桌子
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: 好处:
disadvantages: 缺点:
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_id
和datetime
索引。
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.