Re: Modeling Friendship Relationships

From: Rob Sargent <robjsargent(at)gmail(dot)com>
To: PostgreSQL General <pgsql-general(at)postgresql(dot)org>
Subject: Re: Modeling Friendship Relationships
Date: 2014-11-11 23:28:09
Message-ID: 54629B89.9070905@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 11/11/2014 03:38 PM, Robert DiFalco wrote:
> I have a question about modeling a mutual relationship. It seems basic
> but I can't decide, maybe it is 6 of one a half dozen of the other.
>
> In my system any user might be friends with another user, that means
> they have a reciprocal friend relationship.
>
> It seems I have two choices for modeling it.
>
> 1. I have a table with two columns userOne and userTwo. If John is
> friends with Jane there will be one row for both of them.
> 2. I have a table with two columns owner and friend. If John is
> friends with Jane there will be two rows, one that is {John, Jane} and
> another {Jane, John}.
>
> The first option has the advantage of saving table size. But queries
> are more complex because to get John's friends I have to JOIN friends
> f ON f.userA = "John" OR f.userB = "John" (not the real query, these
> would be id's but you get the idea).
>
> In the second option the table rows would be 2x but the queries would
> be simpler -- JOIN friends f ON f.owner = "John".
>
> There could be >1M users. Each user would have <200 friends.
>
> Thoughts? Do I just choose one or is there a clear winner? TIA!
did you consider a table with id-of-friendship/friend, unique on the
pair, id-of-friendship lists all in the friendship. (Easy aggregate to
get one-line per friendship).

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Steve Crawford 2014-11-11 23:39:48 Re: Modeling Friendship Relationships
Previous Message Nestor A. Diaz 2014-11-11 23:21:44 Autovacuum on partitioned tables in version 9.1