Re: Tricky SELECT question involving subqueries

From: Michael Fuhr <mike(at)fuhr(dot)org>
To: Ben Hallert <ben(dot)hallert(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Tricky SELECT question involving subqueries
Date: 2005-09-11 04:45:33
Message-ID: 20050911044533.GA61717@winnie.fuhr.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Thu, Sep 08, 2005 at 10:02:44AM -0700, Ben Hallert wrote:
> With this in mind, I want to write a query that will list the entries
> in the first table (easy) along with a count() of how many entries in
> the other table start with that path (the hard part).

[...]

> I tried handling this programmaticaly by having a loop that queries
> each path, then does another query below of "SELECT COUNT(*) FROM
> changehistory WHERE UPPER(filespec) LIKE UPPER('$pathspec%')". Each
> count query works fine, but the performance is crippling.

Do you have an expression index on upper(filespec)? That should
speed up queries such as the above. Another possibility might
involve using contrib/ltree. And instead of looping through each
path, you could use an inner or outer join.

CREATE TABLE trackedpaths (pathname ltree);
CREATE TABLE changehistory (filespec ltree);

INSERT INTO trackedpaths (pathname) VALUES ('abc.def');
INSERT INTO trackedpaths (pathname) VALUES ('ghi.jkl');
INSERT INTO trackedpaths (pathname) VALUES ('mno.pqr');

INSERT INTO changehistory (filespec) VALUES ('abc.def.123');
INSERT INTO changehistory (filespec) VALUES ('abc.def.123.456');
INSERT INTO changehistory (filespec) VALUES ('ghi.jkl.789');

SELECT t.pathname, count(c.*)
FROM trackedpaths AS t
LEFT OUTER JOIN changehistory AS c ON c.filespec <@ t.pathname
GROUP BY t.pathname
ORDER BY t.pathname;

pathname | count
----------+-------
abc.def | 2
ghi.jkl | 1
mno.pqr | 0
(3 rows)

--
Michael Fuhr

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Mike Nolan 2005-09-11 05:55:26 Followup to week truncation thread
Previous Message Carlos Henrique Reimer 2005-09-11 00:45:19 Re: locale and encoding