pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Laurette Cisneros <laurette@nextbus.com>
To: pgsql-sql@postgresql.org
Subject: efficient query help
Date: Mon, 22 Apr 2002 11:10:41 -0700 (PDT)
Message-ID: <Pine.LNX.4.44.0204221110140.22730-100000@visor.corp.nextbus.com> (raw)


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 log
 where 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 f
 where (l.id, f.flag) not in
       (select id, flag
          from log
         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?





view thread (4+ messages)  latest in thread

Message-ID: <Pine.LNX.4.44.0204221110140.22730-100000@visor.corp.nextbus.com>
Permalink:  ../Pine.LNX.4.44.0204221110140.22730-100000@visor.corp.nextbus.com/
Also on:    postgresql.org/message-id/Pine.LNX.4.44.0204221110140.22730-100000@visor.corp.nextbus.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: laurette@nextbus.com
  Subject: Re: efficient query help
  In-Reply-To: <Pine.LNX.4.44.0204221110140.22730-100000@visor.corp.nextbus.com>
  References: <Pine.LNX.4.44.0204221110140.22730-100000@visor.corp.nextbus.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