Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id E825A1337B63 for ; Fri, 15 Oct 2010 10:23:30 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.29]) (amavisd-maia, port 10024) with ESMTP id 35276-05 for ; Fri, 15 Oct 2010 13:23:25 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from eagle.jhcloos.com (eagle.jhcloos.com [207.210.242.212]) by mail.postgresql.org (Postfix) with ESMTP id EE9351337B47 for ; Fri, 15 Oct 2010 10:23:22 -0300 (ADT) Received: by eagle.jhcloos.com (Postfix, from userid 10) id ED6C5400E5; Fri, 15 Oct 2010 13:22:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=jhcloos.com; s=eagle; t=1287149001; bh=AQGLJvPb7Vuwn+8JL5QIPW78rLLAWsyeYMMWjmDDzJE=; h=From:To:Subject:In-Reply-To:References:Date:Message-ID: MIME-Version:Content-Type; b=M+jPjJCw50XqaPVsWUjjXmbjJ1eSTXTnMcdQmTx2nt/DjezsozKRRWkKFJ5AUrARi NsRRiB3NjAnFAiYB196X25FtBQm5otJL66cK4JfKGd7MeERhHmDaov5OEWeh4gJ9lV s9Eg7IAD2jdFLnMWAoZ2VilNvHk4Wr+I7AshFd1I= Received: from carbon.jhcloos.org (localhost [127.0.0.1]) by carbon.jhcloos.org (Postfix) with ESMTP id 4CCF61E66BE for ; Fri, 15 Oct 2010 09:57:23 +0000 (UTC) From: James Cloos To: pgsql-sql@postgresql.org Subject: Re: counting related rows In-Reply-To: (James Cloos's message of "Sat, 09 Oct 2010 03:49:12 -0400") References: User-Agent: Gnus/5.110011 (No Gnus v0.11) Emacs/24.0.50 (gnu/linux) Face: iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABHNCSVQICAgIfAhkiAAAAI1J REFUOE+lU9ESgCAIg64P1y+ngUdxhl5H8wFbbM0OmUiEhKkCYaZThXCo6KE5sCbA1DDX3genvO4d eBQgEMaM5qy6uWk4SfBYfdu9jvBN9nSVDOKRtwb+I3epboOsOX5pZbJNsBJFvmQQ05YMfieIBnYX FK2N6dOawd97r/e8RjkTLzmMsiVgrAoEugtviCM3v2WzjgAAAABJRU5ErkJggg== Copyright: Copyright 2009 James Cloos OpenPGP: ED7DAEA6; url=http://jhcloos.com/public_key/0xED7DAEA6.asc OpenPGP-Fingerprint: E9E9 F828 61A4 6EA9 0F2B 63E7 997A 9F17 ED7D AEA6 Date: Fri, 15 Oct 2010 05:57:23 -0400 Message-ID: Lines: 22 MIME-Version: 1.0 Content-Type: text/plain X-Hashcash: 1:30:101015:pgsql-sql@postgresql.org::7XcnNUO9uaDXrGra:0000000000000000000000000000000000009CHan X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-0.318 tagged_above=-5 required=5 tests=BAYES_00=-1.9, DATE_IN_PAST_03_06=1.592, T_RP_MATCHES_RCVD=-0.01 X-Spam-Level: X-Archive-Number: 201010/38 X-Sequence-Number: 34689 After further though, I tried using a function: CREATE OR REPLACE FUNCTION children ( ow integer, parent text) returns integer AS $$ select count(*) as children from m where o = $1 and name ilike $2 || '/%'; $$ LANGUAGE sql; An example call is: select o, name, f1, f2, (select children(o,name) as children) from m where o=3; Which worked, but was no faster than selecting all of the matching ids and iterating through them on the client finding each row's parent count each in its own select. Ie, it took about 1 ks for about 20k rows. So it looks like the real solution is to add a column to track the number of children and update it, for each "parent" row via a trigger whenever a row is added, removed or the path column of a row is changed. -JimC -- James Cloos OpenPGP: 1024D/ED7DAEA6