Received: from davinci.ethosmedia.com (davinci.ethosmedia.com [209.10.40.250]) by postgresql.org (8.11.3/8.11.4) with ESMTP id g02JcVm15004 for ; Wed, 2 Jan 2002 14:38:31 -0500 (EST) (envelope-from josh@agliodbs.com) Received: from [209.10.40.250] (account ) by davinci.ethosmedia.com (CommuniGate Pro WebUser 3.4.7) with HTTP id 539545; Wed, 02 Jan 2002 11:38:28 -0800 From: "Josh Berkus" Subject: Re: Tree structure index usage To: "Aasmund Midttun Godal" , pgsql-sql@postgresql.org X-Mailer: CommuniGate Pro Web Mailer v.3.4.7 Date: Wed, 02 Jan 2002 11:38:28 -0800 Message-ID: In-Reply-To: <20011229200610.29888.qmail@213-145-170-138.dd.nextgentel.com> MIME-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 8bit X-Archive-Number: 200201/2 X-Sequence-Number: 6093 Aasmund, > CREATE TABLE my_tree ( > id SERIAL PRIMARY KEY, > dir INTEGER references my_tree ON DELETE CASCADE ON UPDATE CASCADE, > name TEXT, > UNIQUE(name, dir) > ); > > Then I have a function is_parent(integer, integer), it will tell you > if the second integer (id) is a sub directory of the first > (regardless of the number of levels). > > now obviously is_parent does some queries itself. > > and someone may choose to update the dir value of a row. > > is there any way I can create an efficient index on this > structure/function? Hmmm. Seperate indexes on ID and DIR should be sufficient for most purposes. More complex approaches are only likely to slow things down. How many records do you have? How many levels to the tree structure? For an excellent discussion of efficient construction and queryin of tree structures, see Joe Celko's "SQL for Smarties." -Josh ______AGLIO DATABASE SOLUTIONS___________________________ Josh Berkus Complete information technology josh@agliodbs.com and data management solutions (415) 565-7293 for law firms, small businesses fax 621-2533 and non-profit organizations. San Francisco