Followers/following database structure

后端 未结 4 1967
臣服心动
臣服心动 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:30

    There is a better physical structure than proposed by other answers so far:

    CREATE TABLE follower (
        user_id INT, -- References user.
        follower_id INT,  -- References user.
        PRIMARY KEY (user_id, follower_id),
        UNIQUE INDEX (follower_id, user_id)
    );
    

    InnoDB tables are clustered, so the secondary indexes behave differently than in heap-based tables and can have unexpected overheads if you are not cognizant of that. Having a surrogate primary key id just adds another index for no good reason1 and makes indexes on {user_id, follower_id} and {follower_id, user_id} fatter than they need to be (because secondary indexes in a clustered table implicitly include a copy of the PK).

    The table above has no surrogate key id and (assuming InnoDB) is physically represented by two B-Trees (one for the primary/clustering key and one for the secondary index), which is about as efficient as it gets for searching in both directions2. If you only need one direction, you can abandon the secondary index and go down to just one B-Tree.

    BTW what you did was a violation of the principle of atomicity, and therefore of 1NF.


    1 And every additional index takes space, lowers the cache effectiveness and impacts the INSERT/UPDATE/DELETE performance.

    2 From followee to follower and vice versa.

提交回复
热议问题