Re: counting related rows

From: James Cloos <cloos(at)jhcloos(dot)com>
To: pgsql-sql(at)postgresql(dot)org
Subject: Re: counting related rows
Date: 2010-10-15 09:57:23
Message-ID: m3mxqfvkv8.fsf@carbon.jhcloos.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

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(at)jhcloos(dot)com> OpenPGP: 1024D/ED7DAEA6

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Andreas 2010-10-16 03:23:26 How to collect text-fields from multiple rows ?
Previous Message Rob Sargent 2010-10-12 15:14:34 Re: Duplicates Processing