View Single Post
  #4 (permalink)  
Old 01-15-09, 06:15
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,517
Quote:
Originally Posted by chiefman
it makes sense. anyway I'd like to suppose a scenario. A and B are friends. thus there is a record (A, B, ...) in the table Friends which represents A has a friend B, but how about the other side B has a friend A. should I add another record like (B,A,..) to the table. if so, I think the data is duplicate
You could easily have 2 indexes on the same table
Code:
create clustered index my_main_idx on Friends ( user_id, friends_user_id )
create index my_main_idx on Friends ( friends_user_id, user_id )
Which means you wouldn't have to store the data twice but it does mean you'd have to do 2 searches of the table to get all the friends of A. The biggest problem though is that friendships aren't necessarily a two way thing: say A has declared 2 friends B & C. Then D comes along and declares A as his friend. A will suddenly now find he has 3 friends B,C & D even though he hasn't declared D as his friend. A will probably also find he won't be able to delete D from his list of friends either.

Just my 2c.
Reply With Quote