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: counting related rows
Date: Fri, 08 Oct 2010 18:02:26 -0400
Message-ID: <m3r5g0filx.fsf@carbon.jhcloos.org> (raw)

I have a table which includes a text column containing posix-style
paths.  Ie, matching the regexp "^[^/]+(/[^/]+)*$".

I need to do a query of a number of columns from that table, plus the
count of rows which are "children" of the current row.

The query:

  SELECT count(*) AS nch FROM m WHERE o = (SELECT o FROM m WHERE id=30016)
                AND name ILIKE (SELECT name || '/%' FROM m WHERE id=30016);

selects that extra column given the id.

A view containing all of the columns from m plus a column matching the
above select would cover my needs well.

But I haven't been able to get the syntax right.

The current code (which I did not write) uses one select to get the
values of id it wants, and then iterates through them selecting four
columns from the table and then the above.  As you may imagine, two
selects per row is *slow*.  Maybe 20 rows per second.  I expect a
single, complete select to take < 10 ms.

Again, to be clear, for each row I need the count of other rows which
have the same value for column o and whose name is a child path of the
current row's name.

-JimC
-- 
James Cloos <cloos@jhcloos.com>         OpenPGP: 1024D/ED7DAEA6



view thread (4+ messages)  latest in thread

Message-ID: <m3r5g0filx.fsf@carbon.jhcloos.org>
Permalink:  ../m3r5g0filx.fsf@carbon.jhcloos.org/
Also on:    postgresql.org/message-id/m3r5g0filx.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: <m3r5g0filx.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