I’m going to be stating redevelopment of a particular website which has a social aspect where users can make mutual friendship associations. I’m wonder what a recommended pattern would be for recording these friendships?
Currently we have a ‘friendship’ table, which stores 2 records for every friend connection.
If I request friendship from you, it creates a friend record with these values
me=my_id, you=your_id, approved=0
If you accept my friendship, a new record is created for you with these values
me=your_id, you=my_id, approved=1
and my record is updated to be approved.
Is there a better method for recording this info? We haven’t had any problems with it, but I wonder how gracefully it would extent to say a million records? We could easily have that number of records soon as each person has a multitude of friend records.
When I designed the system I didn’t give it much thought, just wrote it out to work like that. It works simple enough when I need to access my friends (SELECT * FROM friends WHERE me=my_id AND approved=1).
Thanks for pointing that out. i wonder if having to do a table scan on one column would indeed be slower than the 50% overhead of the current pattern?
I don’t mind using a union either. I really only have one query that ever pulls in friends in my model.
How does mysql manage the index data? If I create a dummy table and populate it with a few million records and performance test the different methods would it be accurate, or does it take mysql a while to optimize the index table?
the problem with having only one row is the inefficiency of the query
WHERE friend1 = $my_id OR friend2 = $my_id
the problem here is that the database can use only one column index to optimize this retrieval, so there’s going to be a table scan even if both columns are indexed
the way around it is to write the query as a UNION
SELECT friend2 AS friend
FROM ...
WHERE friend1 = $my_id
...
UNION
SELECT friend1 AS friend
FROM ...
WHERE friend2 = $my_id
but this is a lot clumsier, isn’t it
the other advantage of two rows is that you can easily indicate which person did the asking and which person accepted