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. mac里的docker如何命令行開啟呢?2. 關docker hub上有些鏡像的tag被標記““This image has vulnerabilities””3. docker - 各位電腦上有多少個容器啊?容器一多,自己都搞混了,咋辦呢?4. 關于docker下的nginx壓力測試5. docker容器呢SSH為什么連不通呢?6. nignx - docker內nginx 80端口被占用7. 如何解決Centos下Docker服務啟動無響應,且輸入docker命令無響應?8. docker 下面創建的IMAGE 他們的 ID 一樣?這個是怎么回事????9. angular.js使用$resource服務把數據存入mongodb的問題。10. docker start -a dockername 老是卡住,什么情況?

網公網安備