There has a simple requirement that is query the amount of the Six Degrees relationship from a Friend
table.
The structure of the Friend
is like this:
+----------+---------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+---------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| userId | int(11) | NO | MUL | NULL | |
| friendId | int(11) | NO | | NULL | |
+----------+---------+------+-----+---------+----------------+
Assume I want to know the Six Degrees relationship amount of userId:1
, And I wrote down six queries like this
SELECT friendId FROM Friend WHERE userId = 1
to get the one degree friends.
Then execute
SELECT friendId FROM Friend WHERE userId in (/*above query result*/)
five times.
The problem is not as simple as it looks like, cause I have millions records in Friend table.
There is a strong possibility is the Six Degrees relationship amount of user 1
is greater than six digits, although he/she only have two friends in One Degree relationship.
The number of items in the IN clause is exponentially.
Then the six queries taking more than one minute to get result.
How to optimize this situation?