X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by postgresql.org (Postfix) with ESMTP id F00D39DCAA9 for ; Sun, 4 Dec 2005 18:19:05 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 30134-07 for ; Sun, 4 Dec 2005 18:19:02 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey- Received: from svr2.postgresql.org (svr2.postgresql.org [65.19.161.25]) by postgresql.org (Postfix) with ESMTP id A0A2F9DCA9F for ; Sun, 4 Dec 2005 18:19:03 -0400 (AST) Received: from mailhub2.une.edu.au (mailhub.une.edu.au [129.180.1.142]) by svr2.postgresql.org (Postfix) with ESMTP id BA2D8F0B5E for ; Sun, 4 Dec 2005 22:19:05 +0000 (GMT) Received: from icarus.une.edu.au (icarus.une.edu.au [129.180.47.120]) by mailhub2.une.edu.au (Postfix) with ESMTP id D89FD7F50; Mon, 5 Dec 2005 09:18:55 +1100 (EST) Received: from kgb (unknown [129.180.47.225]) by icarus.une.edu.au (Postfix) with SMTP id 9C4AC3544EE; Mon, 5 Dec 2005 09:18:55 +1100 (EST) Date: Mon, 05 Dec 2005 09:24:51 +1100 From: Klint Gore To: Rodrigo Madera Cc: pgsql-performance@postgresql.org Subject: Re: Faster db architecture for a twisted table. In-Reply-To: <3cf983d0512031500r2a9da709r8eb362806e91f61@mail.gmail.com> References: <3cf983d0512031500r2a9da709r8eb362806e91f61@mail.gmail.com> Message-Id: <43936CB3FB.5E5FKG@129.180.47.120> MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Mailer: Becky! ver 1.25.06 X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=1.332 required=5 tests=[RCVD_IN_BL_SPAMCOP_NET=1.332] X-Spam-Score: 1.332 X-Spam-Level: * X-Archive-Number: 200512/117 X-Sequence-Number: 15938 On Sat, 3 Dec 2005 23:00:21 +0000, Rodrigo Madera wrote: > Imagine a table named Person with "first_name" and "age". > > Now let's make it fancy and put a "mother" and "father" field that is > a reference to the own table (Person). And to get even fuzzier, let's > drop in some siblings: > > CREATE TABLE person( > id bigint PRIMARY KEY, > first_name TEXT, > age INT, > mother bigint REFERENCES person, > father biging REFERENCES person, > siblings array of bigints (don't remember the syntax, but you get the point) > ); > > Well, this is ok, but imagine a search for "brothers of person id > 34". We would have to search inside the record's 'siblings' array. Is > this a bad design? is this going to be slow? Do you need the array at all? alter table person add column gender; select id >from person where gender = 'male' and (mother = (select mother from person where id = 34) OR father = (select father from person where id = 34)) You can change the OR depending if you want half brothers or not > What would be a better design to have these kind of relationships? > (where you need several references to rows inside the table we are). We use that structure (without the sibiling array) for our systems. Siblings are calculated from parents (in our case, livestock, there can be hundreds). You have to be prepared to use recursive functions and make sure that a person doesnt appear anywhere higher in their family tree. klint. +---------------------------------------+-----------------+ : Klint Gore : "Non rhyming : : EMail : kg@kgb.une.edu.au : slang - the : : Snail : A.B.R.I. : possibilities : : Mail University of New England : are useless" : : Armidale NSW 2351 Australia : L.J.J. : : Fax : +61 2 6772 5376 : : +---------------------------------------+-----------------+