Jaynesh Posted July 9, 2011 Share Posted July 9, 2011 Hello When you have a a friend system and you have a table for friends such as friends(my_id, friend_id) Is it ideal to have a duplicate entry for each friend relationship, such as: my_id 1 2 friend_id 2 1 Link to comment https://forums.phpfreaks.com/topic/241486-friend-system-table/ Share on other sites More sharing options...
gizmola Posted July 9, 2011 Share Posted July 9, 2011 Yes if you're using innodb which has a clustered index. If not, it's more of a tossup -- double the data, vs half the data but 2 indexes. With that said, the cost on the size of the indexes is something you only have to pay on insert. Link to comment https://forums.phpfreaks.com/topic/241486-friend-system-table/#findComment-1240472 Share on other sites More sharing options...
Jaynesh Posted July 9, 2011 Author Share Posted July 9, 2011 Ok so if I had one entry how would I adjust my query to affect it both ways. Currently my query pulls a list of all the users friends. This is assuming that there is a double entry. If I have a single entry for e.g user_id friend id 1 2 It treats 2 as a friend of 1 but does not treat 1 as a friend of 2. "SELECT * FROM Users, Friends WHERE user_id = $user and Friends.friend_id = Users.id" Link to comment https://forums.phpfreaks.com/topic/241486-friend-system-table/#findComment-1240474 Share on other sites More sharing options...
gizmola Posted July 9, 2011 Share Posted July 9, 2011 SELECT * FROM Users, Friends WHERE (Friends.user_id = $user AND Users.id = Friends.friend_id) OR (Friends.friend_id = $user AND Users.id = Friend.user_id) You should probably create a load of test data and benchmark both approaches. Also make sure you explain extended on the queries. Link to comment https://forums.phpfreaks.com/topic/241486-friend-system-table/#findComment-1240479 Share on other sites More sharing options...
Jaynesh Posted July 9, 2011 Author Share Posted July 9, 2011 Thank you!! Link to comment https://forums.phpfreaks.com/topic/241486-friend-system-table/#findComment-1240485 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.