设计一个包含用户之间关系的表的好方法是什么?

发布于 2024-12-08 21:38:11 字数 561 浏览 0 评论 0原文

我目前在一个社交网站工作。 (是的,我知道,有一大堆。我并不是想彻底改造 Facebook)

我想知道是否有人可以告诉我我的思维方式是否偏离了方向,或者它是否是实际的方式完毕。

我希望用户能够有朋友。为此,我认为我应该有一个用户表,如下所示: USER

  • uId
  • userName
  • email
  • 等。

这可能应该是 1:N 关系,所以我认为表“联系人”应该包含用户及其朋友的列表,如下所示: CONTACTS

  • uId(来自 USER)
  • FriendId(来自 USER 表)
  • 友谊类型 ENUM[Active、Inactive、Pending]

根据 uId 对此表进行排序是否是一个有效的解决方案,以便查询结果类似于以下内容:
uID |朋友ID
1 | 2
1 | 6
1 | 97
75 | 75 1
75 | 75 34

等等

或者有什么不同的解决方案吗?

I'm currently working on a social networking site. (Yeah, I know, there's a whole bunch of them. I'm not trying to make Facebook all over)

I was wondering if anyone could tell me if my way of thinking is way off, or if it is the way it is actually done.

I want a user to be able to have friends. And, for that, I'm thinking that I should have one usertable like so:
USER

  • uId
  • userName
  • email
  • etc..

This should probably be a 1:N relationship, so I'm thinking that a table "contacts" should hold a list of users and their friends like so:
CONTACTS

  • uId (From USER)
  • FriendId (From USER table)
  • Friendship type ENUM[Active, Inactive, Pending]

Would it be an effective solution to sort this table on uId, so that a query result would look something similar to this:
uID | friendId
1 | 2
1 | 6
1 | 97
75 | 1
75 | 34

etc

Or are there any different solutions to this?

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

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

发布评论

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

评论(2

今天小雨转甜 2024-12-15 21:38:11

如果您只是想选择一组特定的用户朋友,那么查询将很简单,您不必担心性能。

例如:如果您希望返回 UID 8 的朋友的 id,您可以执行以下操作:

Select FriendId FROM TABLE where UID=8;

在您的情况下,由于 UID 列不是唯一的,请确保在此列上有一个索引以允许快速查找(优化表现)。

您可能还需要考虑有关用户朋友的其他哪些数据。例如,仅获取 FriendId 可能没有用,您可能需要名称等。因此您的查询可能看起来更像:

Select FriendId, Users.name FROM Friends JOIN Users ON Users.uid=Friends.FriendId WHERE Friends.UID=8; 

同样,对正确的列进行索引是优化查找的关键,尤其是当您的表大小变大时。

此外,由于与您执行的查找查询数量相比,添加好友的行为可能非常罕见,因此请务必选择提供最快查找速度的数据库引擎。在这种情况下,MyISam 可能是您最好的选择。 MyISam 使用表级锁定进行插入(即较慢的插入),但查找速度很快。

祝你好运!

If you are simply looking to select a specific users set of friends, the query will be straightforward and you won't have to worry about performance.

For example: If you are looking to return the id's of UID 8's friends, you can just do something like:

Select FriendId FROM TABLE where UID=8;

In your case, since the UID column is not unique, make sure to have an Index on this column to allow quick lookup (optimize performance).

You might also want to think about what other data you will need about the users friends. For example its probably not useful to just grab the FriendIds, you probable want names etc. So your query will likely look more like:

Select FriendId, Users.name FROM Friends JOIN Users ON Users.uid=Friends.FriendId WHERE Friends.UID=8; 

Again, having the proper columns indexed is key for optimized lookups, especially once your table size gets big.

Also, since the act of adding friends is likely very uncommon in comparison to the number of lookup queries you do, be sure to choose a database engine that provides the fastest lookup speed. In this case MyISam is probably your best bet. MyISam uses table level locking for inserts (i.e. slower inserts) but the lookups are quick.

Good luck!

机场等船 2024-12-15 21:38:11

我认为最好的方法无疑是创建一个像你建议的那样的表。这将使您能够更好地管理好友,对该表上的好友进行查询,...这将是最好的解决方案。

I think the best way is without doubt creating a table like you proposed. This will allow you to better manage the friends, do query's for friends on this table, ... this would be the best solution.

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