[英]What is the best way of storing following,followers,likes data in mysql
您好,我想創建社交媒體應用程序。我在后端使用 PHP+MYSQL。 我在 MYSQL 用戶表中有標准的用戶列和關注者、關注、帖子和關注者 ID、關注 ID、帖子 ID 列,我用逗號分隔所有 ID。例如
追隨者7
,追隨者2
,followers_id ,42,43,47,48,49,50,51
following_id 12,14
有時我與 PHP explode() function 斗爭,但它運作良好。 那么存儲和尋找這樣的用戶的好方法嗎? 你有什么建議嗎? 如果有人關注 1000 個用戶會發生什么。在這種情況下,我的 PHP 腳本將獲取所有數據(可能會導致 RAM 問題)
我認為一個好方法是使用 mysql 表,其中字段與 id 相關。 關系數據庫允許您在多個表上查詢以獲得許多有趣的數據,例如:誰關注了誰,有多少關注者,與其他用戶共同的關注者。
想象一下這樣的數據庫表結構:
table:users , fields: id, username,num_followers,num_following
table:followers , fields: id, user_id, follower_id , datetime
table:following , fields: id, user_id ,following_id, datetime
查詢示例以獲取 id 為 1 的用戶的所有關注者:
SELECT user_id from followers where user_id=1;
查詢示例以獲取 id 為 1 的用戶的所有關注:
SELECT user_id from following where user_id=1;
查詢示例以獲取 ID 為 1,2 的用戶的所有共同點:
SELECT following_id from following where user_id=1 intersect SELECT following_id from following where user_id=2;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.