dtvgo28624 2012-04-12 14:52
浏览 49
已采纳

PHP / MYSQL /社交网络最佳实践表结构为粉丝

I would like to allow users to follow other users as with Twitter, Instagram etc. Am thinking that this should include table structure something like following:

Users with userid, password, email, fname, lname

Followers with userid1(INT), followstatus(TINYINT), usererid2 (INT)

New row is created when user1 decides to follow user2. Later follow status can be turned off if user1 decides to no longer follow user2. There would be a separate entry if user2 follows user 1

I can tell user1 whom he is following with query to this table. And tell users how many people are following them with count query. Then conditional on this relationship, print out info on user2s actions (followee) to user1 (follower).

Is that best practice? Or is there more to it.... Are there other fields, tables required?

Note: users have another table of people, not necessarily users but I'm thinking follower status should be in separate table.

  • 写回答

2条回答 默认 最新

  • douji4948 2012-04-12 15:01
    关注

    That looks good. One note, though: if your project will really take off, you'll soon grow out of a single machine and will have to shard. That means breaking your user and relation tables into smaller ones. It's a big topic and out of scope here.

    Once you break them, a certain type of queries becomes difficult or impossible.

    For example, now it is simple to query "who follows me".

    select * from followers where user2 = me;
    

    or "whom do I follow?"

    select * from followers where user1 = me;
    

    Once you break the followers table, one of these queries will be impossible (because the data may span multiple tables/databases/servers). To overcome this, you might want to create two tables:

    i_follow (user1 int, user2 int)
    follow_me (user1 int, user2 int)
    

    and insert corresponding records on each new relation.

    This is just tip of the iceberg. Embark on this journey and you'll find many interesting problems to solve :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输