pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: James Cloos <cloos@jhcloos.com>
To: pgsql-sql@postgresql.org
Subject: Re: counting related rows
Date: Fri, 15 Oct 2010 05:57:23 -0400
Message-ID: <m3mxqfvkv8.fsf@carbon.jhcloos.org> (raw)
In-Reply-To: <m3tykverfz.fsf@carbon.jhcloos.org>
References: <m3r5g0filx.fsf@carbon.jhcloos.org>
	<BLU0-SMTP965F0BF4A84C81E4C7400BAC500@phx.gbl>
	<m3tykverfz.fsf@carbon.jhcloos.org>

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 <cloos@jhcloos.com>         OpenPGP: 1024D/ED7DAEA6



view thread (4+ messages)

Message-ID: <m3mxqfvkv8.fsf@carbon.jhcloos.org>
Permalink:  ../m3mxqfvkv8.fsf@carbon.jhcloos.org/
Also on:    postgresql.org/message-id/m3mxqfvkv8.fsf@carbon.jhcloos.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: cloos@jhcloos.com
  Subject: Re: counting related rows
  In-Reply-To: <m3mxqfvkv8.fsf@carbon.jhcloos.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox