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 48D5755B6F for ; Sun, 6 Mar 2005 04:43:32 +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 39925-06 for ; Sun, 6 Mar 2005 04:43:31 +0000 (GMT) Received: from viefep12-int.chello.at (viefep12-int.chello.at [213.46.255.25]) by svr1.postgresql.org (Postfix) with ESMTP id 30C3755965 for ; Sun, 6 Mar 2005 04:43:28 +0000 (GMT) Received: from [213.47.56.254] by viefep12-int.chello.at (InterMail vM.6.01.03.05 201-2131-111-107-20040910) with ESMTP id <20050306044327.BIPE20617.viefep12-int.chello.at@[213.47.56.254]>; Sun, 6 Mar 2005 05:43:27 +0100 Message-ID: <422A8A26.1070704@foo.at> Date: Sun, 06 Mar 2005 05:42:14 +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: pgsql-sql@postgresql.org Subject: Links between rows in a table 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 tagged_above=0 required=5 tests= X-Spam-Level: X-Archive-Number: 200503/80 X-Sequence-Number: 20559 Hi. We are currently designing a web-based application in which users can add other users as "friends". These links are bi-directional, meaning that when A adds B to his friends, he is automatically one of B's friends. Eventually we will have to add a feature that shows how A is is related to some other user E (via B->C->D->...) - similar to the way Friendster, Orkut and others work, but on a much smaller scale (some 5000 users). Probably the most annoying part is that it has to work with different database vendors, including MySQL4 (default install, MyISAM tables, no foreign keys, no stored procedures, no triggers, no views etc). Most of the logic will have to live in the application, and I won't be able to use anything beyond plain SQL. I can see several ways how such links could be modeled in a relational database, but I was wondering if there was some tried-and-true recipe that would spare me from reinventing the wheel. Putting aside for the moment everything but the links, the simplest way of connecting users would be a "friends" table (user_id int, friend_id int). We could get a user's friends with a simple query like this: SELECT friend_id FROM friends WHERE user_id = X UNION SELECT user_id FROM friends WHERE friend_id = X; Is there a better way, or any reason why we should not go that way, especially considering other likely queries such as "friends of friends" or the connection chain mentioned above? We are also thinking of precalculating possible connection chains, or trees, at night (to a certain depth) in order to avoid performance problems in the peak hours. Any ideas on how such precalculated results could be stored and queried efficiently? Thanks in advance, Stefan Weiss