Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id 182521337BA4 for ; Fri, 8 Oct 2010 19:12:51 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.243]) (amavisd-maia, port 10024) with ESMTP id 88490-05 for ; Fri, 8 Oct 2010 22:12:43 +0000 (UTC) X-Greylist: delayed 00:08:49.608649 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 43F561337B9C for ; Fri, 8 Oct 2010 19:12:43 -0300 (ADT) Received: by eagle.jhcloos.com (Postfix, from userid 10) id 73684400E5; Fri, 8 Oct 2010 22:03:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=jhcloos.com; s=eagle; t=1286575432; bh=no83MiiA6E0aYEmPwnUd7MELH0G8/yDRXmQjaJeEYwQ=; h=From:To:Subject:Date:Message-ID:MIME-Version:Content-Type; b=RDnWoesDmGAvExw3oXl9PY+SsG+oaHmSXUYETalBDrNv/yA33UCbvUr4M39+YNacP 85eQm+FHqOe7zcL+M9AcBDM1IY+AZig2hfC2IYU5xrIlxh524PX4xuaAw7aXPqjbnT rOogStI/PYL0BGCYl0NPryG4rRtTiArBA+ATYIBw= Received: from carbon.jhcloos.org (localhost [127.0.0.1]) by carbon.jhcloos.org (Postfix) with ESMTP id 2627E1DF5B7 for ; Fri, 8 Oct 2010 22:02:27 +0000 (UTC) From: James Cloos To: pgsql-sql@postgresql.org Subject: counting related rows 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, 08 Oct 2010 18:02:26 -0400 Message-ID: Lines: 31 MIME-Version: 1.0 Content-Type: text/plain X-Hashcash: 1:30:101008:pgsql-sql@postgresql.org::2QABJHoiRrKQxBXE:000000000000000000000000000000000000JEMzK X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.91 tagged_above=-5 required=5 tests=BAYES_00=-1.9, T_RP_MATCHES_RCVD=-0.01 X-Spam-Level: X-Archive-Number: 201010/25 X-Sequence-Number: 34676 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 OpenPGP: 1024D/ED7DAEA6