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. debian - docker依賴的aufs-tools源碼哪里可以找到啊?2. docker綁定了nginx端口 外部訪問不到3. node.js - nodejs debug問題4. docker 下面創建的IMAGE 他們的 ID 一樣?這個是怎么回事????5. docker網絡端口映射,沒有方便點的操作方法么?6. docker images顯示的鏡像過多,狗眼被亮瞎了,怎么辦?7. docker - 如何修改運行中容器的配置8. docker-compose中volumes的問題9. golang - 用IDE看docker源碼時的小問題10. docker-machine添加一個已有的docker主機問題
