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查詢出所有關注我的人(粉絲)。
當然如果不要擴展性或數據很小,那兩個字段正著查所有我關注的人,反著查所有的關注我的人(粉絲)
相關文章:
1. 致命錯誤: Class ’appfacadeTest’ not found2. html5 - 如何實現帶陰影的不規則容器?3. objective-c - iOS開發支付寶和微信支付完成為什么跳轉到了之前開發的一個app?4. css - 移動端字體設置問題5. python - 管道符和ssh傳文件6. javascript - 循環嵌套多個promise應該如何實現?7. mysql優化 - 關于mysql分區8. 請教各位大佬,瀏覽器點 提交實例為什么沒有反應9. css3 - rem布局下,用戶瀏覽器的最小字號是12px怎么辦?10. javascript - ionic2 input autofocus 電腦成功,iOS手機鍵盤不彈出
