best way to store 1:1 user relationships in relational database

后端 未结 4 1032
独厮守ぢ
独厮守ぢ 2021-02-01 09:24

What is the best way to store user relationships, e.g. friendships, that must be bidirectional (you\'re my friend, thus I\'m your friend) in a rel. database, e.g. MYSql?

<
相关标签:
4条回答
  • 2021-02-01 10:03

    Use a key value store, such as Cassandra for example.

    0 讨论(0)
  • 2021-02-01 10:12

    I would have a link table for friends, or whatever, with 2 columns both being PK's, and both being FK's to the User table.

    Both columns would be the UID, and you would have two rows per friend relationship (A,B and B,A). As long as both columns are PK's, it should still be in normal format (although others are free to correct me on this)

    Its a little more complex of a query, but nothing that can't be abstracted away by a stored procedure or some business logic, and its in Normal Format, which is usually nice to have.

    0 讨论(0)
  • 2021-02-01 10:12

    Using double rows, while it creates extra data, will greatly simplify your queries and allow you to index smartly. I also remember seeing info on Twitter's custom MySQL solution wherein they used an additional field (friend #, basically) to do automatic limiting and paging. It seems pretty smooth: https://blog.twitter.com/2010/introducing-flockdb

    0 讨论(0)
  • 2021-02-01 10:13

    You could check which of the two user_id's is the lowest and store them in a specific order. This way you don't need double rows for one friendship and still keep your queries simple.

    user_id_low | user_id_high

    a simple query to check if you're already friends with someone would be:

    <?php
    $my_id = 2999;
    $friend_id = 500;
    
    $lowest = min($my_id, $friend_id);
    $highest= max($my_id, $friend_id);
    
    query("SELECT * FROM friends WHERE user_id_low=$lowest AND user_id_high=$highest");
    ?>
    

    Or you could find the lowest/higest userid using mysql

    <?php
    query("SELECT * FROM friends WHERE user_id_low=LEAST($my_id, $friend_id) AND user_id_high=GREATEST($my_id, $friend_id)");
    ?>
    

    And to get all your friends id's

    <?php
    
    query("SELECT IF(user_id_low=$my_id,user_id_high,user_id_low) AS friend_id FROM friends WHERE $my_id IN (user_id_low, user_id_high)");
    
    ?>
    
    0 讨论(0)
提交回复
热议问题