在社交网络数据库中存储好友

发布于 2024-11-19 04:01:35 字数 358 浏览 1 评论 0原文

为了在社交网络中存储朋友关系,最好使用另一个包含列relationship_id、user1_id、user2_id、time_created、pending的表,还是应该将已确认朋友的 user_id 序列化/内爆为单个长字符串并存储除了其他用户详细信息(例如用户 ID、姓名、出生日期、地址)以及类似于 facebook 的仅喜欢 5000 个朋友的限制之外?

还有更好的方法吗?第一种方法将创建一个巨大的表!第二个有一列非常长的字符串...

在每个用户的个人资料页面上,需要从数据库中检索他的所有朋友才能显示类似于 facebook 的 30 个朋友,所以我认为第一种方法是使用单独的表会造成大量的数据库查询吗?

For storing friends relationships in social networks, is it better to have another table with columns relationship_id, user1_id, user2_id, time_created, pending or should the confirmed friend's user_id be seralized/imploded into a single long string and stored along side with the other user details like user_id, name, dateofbirth, address and limit to like only 5000 friends similar to facebook?

Are there any better methods? The first method will create a huge table! The second one has one column with really long string...

On the profile page of each user, all his friends need to be retrieved from database to show like 30 friends similar to facebook, so i think the first method of using a seperate table will cause a huge amount of database queries?

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

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

发布评论

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

评论(2

我一直都在从未离去 2024-11-26 04:01:35

正确的方法是拥有成员表(显然)和第二个朋友关系表。

您永远不应该将外键存储在这样的字符串中。有什么意义?您无法加入它们、对它们进行排序、对它们进行分组或任何其他证明拥有关系数据库的理由。

如果我们假设 Member 表看起来像这样:

MemberID int Primary Key
Name varchar(100) Not null
--etc

那么你的 Friendship 表应该看起来像这样:

Member1ID int Foreign Key -> Member.MemberID
Member2ID int Foreign Key -> Member.MemberID
Created datetime Not Null
--etc

然后,你可以将这些表连接在一起以获取朋友列表

SELECT m.*
FROM Member m
RIGHT JOIN Friendship f ON f.Member2ID = m.MemberID
WHERE f.MemberID = @MemberID

(这是特定的 SQL Server 语法,但我认为它非常接近 MySQL 。@MemberID 是一个参数)

这总是比分割字符串并进行 30 个额外的 SQL 查询来提取相关数据要快。

The most proper way to do this would be to have the table of Members (obviously), and a second table of Friend relationships.

You should never ever store foreign keys in a string like that. What's the point? You can't join on them, sort on them, group on them, or any other things that justify having a relational database in the first place.

If we assume that the Member table looks like this:

MemberID int Primary Key
Name varchar(100) Not null
--etc

Then your Friendship table should look like this:

Member1ID int Foreign Key -> Member.MemberID
Member2ID int Foreign Key -> Member.MemberID
Created datetime Not Null
--etc

Then, you can join the tables together to pull a list of friends

SELECT m.*
FROM Member m
RIGHT JOIN Friendship f ON f.Member2ID = m.MemberID
WHERE f.MemberID = @MemberID

(This is specifically SQL Server syntax, but I think it's pretty close to MySQL. The @MemberID is a parameter)

This is always going to be faster than splitting a string and making 30 extra SQL queries to pull the relevant data.

淡忘如思 2024-11-26 04:01:35

如方法 1 所示,单独表格。
方法 2 不好,因为你每次都必须反序列化它并且无法对其进行 JOINS;如果用户更改了他的姓名、电子邮件或其他属性,那么更新将是一场噩梦。

当然该表会很大,但您可以在 Member11_id 上对其进行索引,将外键设置回您的用户表,并且可以具有静态行大小,甚至可以限制单个用户可以拥有的朋友数量。我认为如果你做得正确的话,这不会是 mysql 的问题;即使您的关系表中有几百万行。

Separate table as in method 1.
method 2 is bad because you would have to unserialize it each time and wont be able to do JOINS on it; plus UPDATE's will be a nightmare if a user changes his name, email or other properties.

sure the table will be huge, but you can index it on Member11_id, set the foreign key back to your user table and could have static row sizes and maybe even limit the amount of friends a single user can have. I think it wont be an issue with mysql if you do it right; even if you hit a few million rows in your relationship table.

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