mysql - 新浪微博中的關注功能是如何設計表結構的?
問題描述
問題解答
回答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查詢出所有關注我的人(粉絲)。
當然如果不要擴展性或數據很小,那兩個字段正著查所有我關注的人,反著查所有的關注我的人(粉絲)
相關文章:
