Re: Tricky SELECT question involving subqueries

Поиск
Список
Период
Сортировка
От Michael Fuhr
Тема Re: Tricky SELECT question involving subqueries
Дата
Msg-id 20050911044533.GA61717@winnie.fuhr.org
обсуждение исходный текст
Ответ на Tricky SELECT question involving subqueries  ("Ben Hallert" <ben.hallert@gmail.com>)
Список 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

В списке pgsql-general по дате отправления:

Предыдущее
От: Carlos Henrique Reimer
Дата:
Сообщение: Re: locale and encoding
Следующее
От: Mike Nolan
Дата:
Сообщение: Followup to week truncation thread