mysql - 新浪微博中的關(guān)注功能是如何設(shè)計(jì)表結(jié)構(gòu)的?
問題描述
問題解答
回答1:個(gè)人簡單猜測,如有雷同,純屬巧合!有錯(cuò)誤請指正!
user_relation - 用戶關(guān)系表user_id - 用戶IDfollower_id - 被關(guān)注者用戶IDrelation_type - 關(guān)系類型,1=關(guān)注 2=粉絲
業(yè)務(wù)邏輯處理
1 用戶A關(guān)注了用戶B
插入兩條記錄
insert user_relation(user_id,follower_id,relation_type) values(a_id,b_id,1);//增加一個(gè)關(guān)注的人insert user_relation(user_id,follower_id,relation_type) values(b_id,a_id,2);//增加一個(gè)粉絲
2 查用戶A關(guān)注的所有用戶
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等等邏輯以此類推。。。。
設(shè)計(jì)理由
考慮到擴(kuò)展性,數(shù)據(jù)量大了必定分庫分表,一般按user_id取模等等算法拆分,所以沒辦法用follower_id查詢出所有關(guān)注我的人(粉絲)。
當(dāng)然如果不要擴(kuò)展性或數(shù)據(jù)很小,那兩個(gè)字段正著查所有我關(guān)注的人,反著查所有的關(guān)注我的人(粉絲)
相關(guān)文章:
1. javascript - immutable配合react提升性能?2. javascript - sublime快鍵鍵問題3. javascript - nodejs關(guān)于進(jìn)程間發(fā)送句柄的一點(diǎn)疑問4. Apache 已經(jīng)把網(wǎng)站根目錄的改為allow from all了,但是服務(wù)器還是不能訪問?5. 實(shí)現(xiàn)bing搜索工具urlAPI提交6. javascript - 移動(dòng)端上不能實(shí)現(xiàn)拖拽布局嗎?7. vue.js - Vue 如何像Angular.js watch 一樣監(jiān)聽數(shù)據(jù)變化8. phpstudy8.1支持win11系統(tǒng)嗎?9. 配置Apache時(shí),添加對(duì)PHP的支持時(shí)語法錯(cuò)誤10. css - 寫頁面遇到個(gè)布局問題,求大佬們幫解答,在線等,急!~

網(wǎng)公網(wǎng)安備