数据库设计-好友活动

发布于 2024-09-29 14:53:40 字数 410 浏览 6 评论 0原文

目前我正在设计一个小型的 twitter/facebook 类型的系统,用户应该能够看到他的朋友的最新活动。

我正在使用 ASP.NET 和 MySQL 数据库。

我的好友关系表如下:

|友谊id|朋友1|朋友2|已确认|

上表中的 Friend1 和 Friend2 是用户 ID。

用户活动表设计如下:

|activityId|用户 ID|活动|日期|

现在,我正在寻找查询用户最新 50 条好友活动的最佳方法。

例如,假设 Tom 登录系统,他应该能够看到他所有朋友的最新 50 条活动。

任何关于最佳实践的指示、查询或任何信息都值得赞赏。

Currently I am designing a small twitter/facebook kind of system, where in a user should be able to see his friends latest activities.

I am using ASP.NET with MySQL database.

My Friendships table is as follows:

|Friendshipid|friend1|Friend2|confirmed|

Friend1 and Friend2 in the above table are userids.

User activities table design following:

|activityId|userid|activity|Dated|

Now, I am looking for best way to query the latest 50 friend activities for a user.

For example, let's say if Tom logs into the system, he should be able to see latest 50 activities among all his friends.

Any pointers on the best practices, a query or any information is appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

§对你不离不弃 2024-10-06 14:53:40

这很大程度上取决于 Friendships 表中存储的数据。例如,Friend1 和 Friend2 字段的存储顺序是什么?如果对于字段 (friend1,friend2),元组 (1, 2) 存在,那么 (2, 1) 也存在吗?

如果情况并非如此,那么这应该有效:

SELECT activities.* 
FROM Activities 
    INNER JOIN Friendships ON userid = friend1 OR userid = friend2 
WHERE activity.userid != [my own id]
    AND confirmed = TRUE
LIMIT 50;

It largely depends on what data is stored in the Friendships table. For example, what order are the Friend1 and Friend2 fields stored in? If, for the fields (friend1, friend2) the tuple (1, 2) exists, will (2, 1) exist also?

If this is not the case, then this should work:

SELECT activities.* 
FROM Activities 
    INNER JOIN Friendships ON userid = friend1 OR userid = friend2 
WHERE activity.userid != [my own id]
    AND confirmed = TRUE
LIMIT 50;
嗼ふ静 2024-10-06 14:53:40

如果您有数据库性能问题,您可以重新定义Friendship表,如下所示:

friendshipid, userid, friendid, confirmed

当您查询最新50个活动时,SQL将是:

SELECT act.*
FROM Activities AS act
    INNER JOIN
    Friendships AS fs
    ON fs.friendid = act.userid
        AND fs.user_id = 'logon_user_id'
        AND confirmed = TRUE
ORDER BY act.dated DESC
LIMIT 50;

如果Friendships(userid)列上有索引,则它将使数据库有机会优化查询。

重新定义的友谊表需要在友谊发生时创建两个元组,但它仍然遵循业务规则,并且在需要时具有性能优势。

If you have database performance concern, you may redefine the friendship table as following:

friendshipid, userid, friendid, confirmed

When you query the latest 50 activities, the SQL would be:

SELECT act.*
FROM Activities AS act
    INNER JOIN
    Friendships AS fs
    ON fs.friendid = act.userid
        AND fs.user_id = 'logon_user_id'
        AND confirmed = TRUE
ORDER BY act.dated DESC
LIMIT 50;

And if there is a index on Friendships(userid) column, it would give the database the chance to optimize the query.

The friendship table redefined needs to create two tuples when a friendship occur, but it still obey the rule of business, and, has performance benefit when you need it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文