Followers/following database structure

后端 未结 4 1961
臣服心动
臣服心动 2021-01-31 05:52

My website has a followers/following system (like Twitter\'s). My dilemma is creating the database structure to handle who\'s following who.

What I came up with was crea

4条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-31 06:48

    No, the approach you describe has a few problems.

    First, storing multiple data points as comma-separated strings has a number of issues. It's difficult to join on (and while you can join using like it will slow down performance) and difficult and slow to search on, and can't be indexed the way you would want.

    Second, if you store both a list of followers and a list of people following, you have redundant data (the fact that A is following B will show up in two places), which is both a waste of space, and also creates the potential of data getting out-of-sync (if the database shows A on B's list of followers, but doesn't show B on A's list of following, then the data is inconsistent in a way that's very hard to recover from).

    Instead, use a join table. That's a separate table where each row has a user id and a follower id. This allows things to be stored in one place, allows indexing and joining, and also allows you to add additional columns to that row, for example to show when the following relationship started.

提交回复
热议问题