您的位置:首页技术文章
文章详情页

mysql - 新浪微博中的关注功能是如何设计表结构的?

【字号: 日期:2022-06-14 18:21:28浏览:70作者:猪猪

问题描述

问题解答

回答1:

个人简单猜测,如有雷同,纯属巧合!有错误请指正!

user_relation - 用户关系表user_id - 用户IDfollower_id - 被关注者用户IDrelation_type - 关系类型,1=关注 2=粉丝

业务逻辑处理

1 用户A关注了用户B

插入两条记录

insert user_relation(user_id,follower_id,relation_type) values(a_id,b_id,1);//增加一个关注的人insert user_relation(user_id,follower_id,relation_type) values(b_id,a_id,2);//增加一个粉丝

2 查用户A关注的所有用户

select * from user_relation where user_id=a_id and relation_type=1

3 查用户A有多少粉丝

select * from user_relation where user_id=a_id and relation_type=2

4,5等等逻辑以此类推。。。。

设计理由

考虑到扩展性,数据量大了必定分库分表,一般按user_id取模等等算法拆分,所以没办法用follower_id查询出所有关注我的人(粉丝)。

当然如果不要扩展性或数据很小,那两个字段正着查所有我关注的人,反着查所有的关注我的人(粉丝)

标签: 微博
相关文章: