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. css3 - [CSS] 動畫效果 3D翻轉bug2. python - Django分頁和查詢參數的問題3. javascript - 百度echarts series數據更新問題4. MySQL客戶端吃掉了SQL注解?5. javascript - JS設置Video視頻對象的currentTime時出現了問題,IE,Edge,火狐,都可以設置,反而chrom卻...6. php自學從哪里開始?7. python小白的基礎問題 關于while循環的嵌套8. 求大神幫我看看是哪里寫錯了 感謝細心解答9. phpstady在win10上運行10. javascript - 圖片能在網站顯示,但控制臺仍舊報錯403 (Forbidden)
