在社交网络数据库中存储好友
为了在社交网络中存储朋友关系,最好使用另一个包含列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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最正确的方法是拥有成员表(显然)和第二个朋友关系表。
您永远不应该将外键存储在这样的字符串中。有什么意义?您无法加入它们、对它们进行排序、对它们进行分组或任何其他证明拥有关系数据库的理由。
如果我们假设 Member 表看起来像这样:
那么你的 Friendship 表应该看起来像这样:
然后,你可以将这些表连接在一起以获取朋友列表
(这是特定的 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:
Then your Friendship table should look like this:
Then, you can join the tables together to pull a list of friends
(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.
如方法 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.