X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 88D7A54F2C for ; Sun, 6 Mar 2005 20:09:08 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 54745-03 for ; Sun, 6 Mar 2005 20:09:06 +0000 (GMT) Received: from viefep14-int.chello.at (viefep14-int.chello.at [213.46.255.13]) by svr1.postgresql.org (Postfix) with ESMTP id 5C7D254D70 for ; Sun, 6 Mar 2005 20:09:04 +0000 (GMT) Received: from [213.47.56.254] by viefep14-int.chello.at (InterMail vM.6.01.03.05 201-2131-111-107-20040910) with ESMTP id <20050306200904.JQCX21537.viefep14-int.chello.at@[213.47.56.254]>; Sun, 6 Mar 2005 21:09:04 +0100 Message-ID: <422B6315.1050904@foo.at> Date: Sun, 06 Mar 2005 21:07:49 +0100 From: Stefan Weiss Organization: Foo Orbital Operations User-Agent: Mozilla Thunderbird 1.0 (X11/20041206) X-Accept-Language: en-us, en MIME-Version: 1.0 To: PFC Cc: Bruno Wolff III , pgsql-sql@postgresql.org Subject: Re: Links between rows in a table References: <422A8A26.1070704@foo.at> <20050306174201.GA19019@wolff.to> <422B4B9A.8020606@foo.at> In-Reply-To: Content-Type: text/plain; charset=ISO-8859-15 Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.013 tagged_above=0 required=5 tests=AWL X-Spam-Level: X-Archive-Number: 200503/89 X-Sequence-Number: 20568 On 2005-03-06 20:26, PFC wrote: > Because your relation is symmetric, you should not name them "user" and > "friend". A good point, thank you. > user_id_1 < user_id_2 means : > - a user can't be his own friend > - only one row per friend > - when you want to know if A is friend of B, no need to make two selects, > just select where user_id_1 = min(user_id_A, user_id_B) AND user_id_2 = > max(user_id_A, user_id_B) This is what we were planning to do on the application side, but a CHECK constraint is even better. It will be used and enforced by those DB engines that understand it, and ignored by the one engine that doesn't. > To get the list of friends for a user, you still need the union, but that > is no real problem. Making two queries will be marginally slower than one > query on a bigger table, but youu save precious cache space, so in the end > it could be faster. Thank you for your insight. We will rename the columns, add the CHECK and go ahead with this setup. regards, stefan weiss