MySQL pays attention to how to design the fan system

clipboard.png
the current data table is designed like this
suppose that the table name is user_fans user A user id is 1 user B user id is 2
user A follows user B and inserts data, the four fields are respectively 1 120
user B follows user An and the data is inserted, the four fields are 22 10
then they are mutual fans. I changed the status fields of the two records from 0 to 1

.

user A views his fan list SQL is select fans_id, status from user_fans where user_id = 1
then the status field can be used to determine the status of the relationship between user A"s fans and user A

but there is a problem, for example, when user A looks at user B"s fan list
, the relationship between user B"s fan list and user An is not clear.
can only query all the user id followed by user A first, and then traverse user B"s fan list (suppose 20 people are checked out. Traversing the 20-length array)
in the foreach loop to determine whether the id of user B"s fans is in the array followed by user A
if the amount of data is small, there should be no performance problem
but for example, if user A follows 3000 people, then there will be 3000 ID,
in the user array followed by user A, so that user A will be slow to check other people"s fan list every time, causing performance problems. Is there a good way to solve this problem?
see some articles on the Internet saying that fans and followers of such systems should use redis,. If how to use redis, what data type should be used?

Jan.13,2022

your way of thinking is the opposite. When judging the relationship between the user in the b fan list and a, you can only judge the user of the current page. You will not first find out all the followings of an and then judge the relationship
. For example, on the first page of the b fan list, there are 20 users. Just check the relationship between these 20 users and a, instead of finding all the followings of a, and then traverse the fan list judgment relationship of b

. < hr >

read the dialogue in the question comments, and then add
1. For a normal user, the number of followings will not increase indefinitely, generally normal at the level of 10 to 100, and more at the thousand level
2. Generally speaking, for the behavior that the user actively follows, it will set an upper limit of attention
under the premise of 1 and 2, the follow list of user an is of the order of magnitude of controllable and small
3. Instead of finding out all of a's fan list and then traversing it 20 times, it is to determine whether the 20 users are in a's follow list. The two are different

.
but there is a problem, for example, when user A looks at user B's fan list
, there is no way to determine the relationship between user B's fan list and user A.
can only query all the user id that user A follows first. Then iterate through the fan list of user B
and determine in the loop whether the id of each fan of user B is in the array followed by user A.
if the amount of data is small, there should be no performance problem
var $Afans = select fans_id from user_fans where user_id = A.id
var $AandBfans = select fans_id form user_fans where fans_id in ($Afans) and user_id = B.id

mysql can still hold up with such a query

secondly, user A views other people's fan lists and asynchronously loads the relationship between people in other people's fan lists and A. the page loads quickly, which can improve the user's experience.


first, suppose there are really 300000 fans, then when checking how many fans b has, proper paging will not find all at once; you know the efficiency brought by paging.
second, as said upstairs, if a looks at the fan list of b, it only pays attention to whether the fans of b are fans of a, not whether the fans of b are fans of a. This is the relationship of 1RV n, don't make it into a relationship of NRV m;
third, all three fields are indexed, so the efficiency will be faster. Don't underestimate the performance of mysql innodb.

Menu