X-Original-To: pgsql-sql@postgresql.org Received: from spampd.localdomain (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id E73DD475A4B for ; Mon, 24 Mar 2003 03:17:16 -0500 (EST) Received: from www.fastmail.fm (www.fastmail.fm [66.111.4.2]) by postgresql.org (Postfix) with ESMTP id 46E134758F1 for ; Mon, 24 Mar 2003 03:17:14 -0500 (EST) Received: from smtp.us2.messagingengine.com (server2.internal [10.202.2.133]) by localhost.localdomain (Postfix) with ESMTP id 8FF0524E70 for ; Mon, 24 Mar 2003 03:17:14 -0500 (EST) Received: from 127.0.0.1 ([127.0.0.1] helo=smtp.us2.messagingengine.com) by fastmail.fm with SMTP; Mon, 24 Mar 2003 03:17:14 -0500 Received: by smtp.us2.messagingengine.com (Postfix, from userid 99) id 9820E4F4B0; Mon, 24 Mar 2003 03:17:14 -0500 (EST) Content-Disposition: inline Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="Big5" MIME-Version: 1.0 X-Mailer: MIME::Lite 1.2 (F2.71; T1.001; A1.51; B2.12; Q2.03) From: "CN" To: pgsql-sql@postgresql.org Date: Mon, 24 Mar 2003 00:17:14 -0800 X-Epoch: 1048493834 X-Sasl-enc: yVKTsx93YDW13494O3g1Iw Subject: Which Approach Performs Better? Message-Id: <20030324081714.9820E4F4B0@smtp.us2.messagingengine.com> X-Spam-Status: No, hits=-3.0 required=5.0 tests=BAYES_10,FORGED_RCVD_TRAIL,UPPERCASE_25_50 version=2.50 X-Spam-Level: X-Spam-Checker-Version: SpamAssassin 2.50 (1.173-2003-02-20-exp) X-Archive-Number: 200303/535 X-Sequence-Number: 12574 Hi! I have a tree table: CREATE TABLE tree ( CONSTRAINT fktree FOREIGN KEY (parent) REFERENCES tree (dept), dept int primary key, --department parent int ); insert into tree values(1,1); insert into tree values(2,1); insert into tree values(3,2); and a history table: CREATE TABLE history ( CONSTRAINT fkhistory FOREIGN KEY (dept) REFERENCES tree (dept), dept int primary key, --department amount int ); insert into history values(1,100); insert into history values(2,200); insert into history values(3,300); My purpose is to retrieve the amount detail of department "1" and all departments under it. I have come out with 2 approaches: APPROACH A: --Returns TRUE if department $2 reports to department $1. CREATE FUNCTION IsChild(TEXT,TEXT) RETURNS BOOLEAN AS ' DECLARE p ALIAS FOR $1; --parent c ALIAS FOR $2; --child vparent INT; BEGIN IF c = p THEN RETURN TRUE; END IF; SELECT parent INTO vparent FROM tree WHERE dept=c; IF NOT FOUND THEN RETURN FALSE; ELSE RETURN IsChild(p,vparent); END IF; END' LANGUAGE 'plpgsql' STABLE; SELECT amount FROM history WHERE IsChild(1,dept); --------------------- APPROACH B: (Assuming the number of layers of this tree is predicatable. Let's take 3 layers as an example.) SELECT amount FROM history WHERE dept=1 OR dept IN (SELECT dept FROM tree WHERE parent=1) OR dept IN (SELECT dept FROM tree WHERE parent IN (SELECT dept FROM tree WHERE parent=1)); Both queries return amount -------- 100 200 300 (3 rows) APPROACH A is obviously easier to implement. My question is which approach gets better performance when the number of rows in history and the layers in tree grows? I don't intend to apply "Joy's worm" tree algorism as it is too complicate to me to understand. Thank you in advance for any input! Regards, CN -- http://www.fastmail.fm - A no graphics, no pop-ups email service