pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Alex Rice <alrice@ARCplanning.com>
To: Bruno Wolff III <bruno@wolff.to>
Cc: pgsql-sql@postgresql.org
Subject: Re: 2 simple SQL questions: optimizing aggegate query
Date: Tue, 6 May 2003 13:28:05 -0600
Message-ID: <DC3C990C-7FF8-11D7-96E3-000393529642@ARCplanning.com> (raw)
In-Reply-To: <20030506012818.GA8384@wolff.to>


On Monday, May 5, 2003, at 07:28  PM, Bruno Wolff III wrote:

>> I am converting some existing query examples that use the form
>> SUM( dict.word IN ( word list...))
>> which doesn't work with pgsql.
>
> You want to do this outside the sum, not inside (and change sum to
> count). For example:
> select count(dict.word) from dict where word in (word list ...);
> (If word list is a select rather than a list of constants, you 
> currently
> want to use exists instead for performance reasons. In 7.4 that won't
> be a problem.)
>
> By putting things in the where close you don't have to examine all of
> the rows for each word - url pair. This may cut out a lot of rows that
> need to be examined if there is an index on dict.word and the number of
> words in a url is large relative to the number being searched on.
>

Bruno: Thanks! Using your advice, my queries are now < 1 sec! Using 
this form

SELECT url_id, url.url, url.txt, url.content_type,
	url.docsize, count(dict.word) AS rank
FROM url,dict WHERE word IN ('word1', 'word2', ...)
AND url.rec_id = dict.url_id
GROUP BY url_id, url, txt, content_type, docsize
ORDER BY rank DESC;

I have also upgraded to pgsql 7.3.2, and the query I posted yesterday 
"sum( case ... ) " is a lot faster as well (about 2 seconds now). So I 
think maybe there was also something wrong with my indices yesterday- 
in addition to there being something wrong with my SQL. :-)

Thanks for the SQL advice.

Alex Rice, Software Developer
Architectural Research Consultants, Inc.
alrice@ARCplanning.com
alrice@swcp.com




view thread (5+ messages)

Message-ID: <DC3C990C-7FF8-11D7-96E3-000393529642@ARCplanning.com>
Permalink:  ../DC3C990C-7FF8-11D7-96E3-000393529642@ARCplanning.com/
Also on:    postgresql.org/message-id/DC3C990C-7FF8-11D7-96E3-000393529642@ARCplanning.com

 · 

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 from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-sql@postgresql.org
  Cc: alrice@ARCplanning.com, bruno@wolff.to
  Subject: Re: 2 simple SQL questions: optimizing aggegate query
  In-Reply-To: <DC3C990C-7FF8-11D7-96E3-000393529642@ARCplanning.com>
  References: <20030506012818.GA8384@wolff.to> <DC3C990C-7FF8-11D7-96E3-000393529642@ARCplanning.com>

* 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