How to implement a friendship structure on a website

I developed a friendship system in which the user table has a friendship column. In this column, I store the user ID in an array, sharing it with a coma. Example: 1,5,15,20

So this means that the user is a friend with other users who has 1,5,15,20 as an identifier. But there is another way to do this. Create a friendship table and save everything there. Like in this example:

https://stackoverflow.com/questions/25101089/mutual-friendship-rails-4

How to implement a friendship model in Rails 3 for a social networking application?

My question is, which way is the best way, like efficiency and speed? Using an array column or a separate table?

Thanks.

+4
source share
1 answer

Efficiency and speed depend on how you use this data.

1#You have a model Userand a column "friends" with user_id friends in the same table.

It is fast to read and does not require a large amount of memory. But you have to decide in which row of the table the data should be stored, because you absolutely do not want to store / manage the same data in two different rows of the table. If you only need a method User.friends, this is a quick and efficient way to store identifiers. But if you want to have methods such as User.is_friends_with, you need to query each row in your user table. It will be neither fast nor effective.

2# User 'is_friends_with' user_id .

, #1, User.is_friends_with , User.friends .

3# User Friendship

, . Friendship (, , ). 100 , 100 .

, , #3. , .

+1
source

Source: https://habr.com/ru/post/1608837/


All Articles