efficient query help - Mailing list pgsql-sql

From Laurette Cisneros
Subject efficient query help
Date
Msg-id Pine.LNX.4.44.0204221110140.22730-100000@visor.corp.nextbus.com
Whole thread Raw
Responses Re: efficient query help  (Clinton Adams <clinton@vote-smart.org>)
Re: efficient query help  ("Joel Burton" <joel@joelburton.com>)
List pgsql-sql
Hi all,

I have crafted the following query which returns the results I want but
runs extremely slow (I'm sure it's the "not in" part that does it) since
the tables can contain large amounts of data.

Is there a better, more efficient way to build this query?  

It's important that I return zero counts for id flag combinations that do not 
have an entry in the log table hence the union with the "not in":

id is a text field and flgtime is a timestamp.  flags is a table that contains 
a single column with a row for each unique flag (text).

select id, flag, count(*) as count from logwhere date(flgtime) = '2002-04-16' and flag is not null
group by id, flag
union
select distinct l.id, f.flag, 0 as count from log l, flags fwhere (l.id, f.flag) not in      (select id, flag
fromlog        where date(flgtime) = '2002-04-16'          and fag is not null)
 
group by l.id, f.flag
;

Thanks for any suggestions.

-- 
Laurette Cisneros
Database Roadie
(510) 420-3137
NextBus Information Systems, Inc.
www.nextbus.com
Where's my....bus?




pgsql-sql by date:

Previous
From: Tom Lane
Date:
Subject: Re: Is this a BUG? Is there anyone has the same problem?
Next
From: Clinton Adams
Date:
Subject: Re: efficient query help