pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Moray McConnachie <moray.mcconnachie@computing-services.oxford.ac.uk>
To: Andrew Perrin - Demography <aperrin@demog.berkeley.edu>
To: pgsql-sql@postgresql.org
Subject: Re: Speeding up a query
Date: Sat, 8 Apr 2000 12:53:21 +0100
Message-ID: <007d01bfa151$0a23bb40$01c8a8c0@malthouse.private.net> (raw)
References: <Pine.LNX.4.10.10004071209290.7254-100000@famine.DEMOG.Berkeley.EDU>


----- Original Message -----
From: Andrew Perrin - Demography <aperrin@demog.berkeley.edu>
To: <pgsql-sql@postgresql.org>
Sent: Friday, April 07, 2000 8:12 PM
Subject: [SQL] Speeding up a query


>
> select last_name from personal_data where id in (
> select distinct id from event_detail, event_universe where
> event_detail.eventtypeid = event_universe.eventtypeid and
> event_universe.studentstatus = 'Student' and
> event_detail.date_effective <= datetime(date('1-jan-1998')))
> and
> id not in (
> select distinct id from event_detail, event_universe where
> event_detail.eventtypeid = event_universe.eventtypeid and
> event_universe.studentstatus = 'NotStudent' and
> event_detail.date_effective <= datetime(date('1-jun-1998')))

Assuming you have indices set up on all appropriate fields, and that you
replace = by LIKE for text fields (don't know if that makes any speed
difference, but it makes it standard SQL), then you need to modify your
query to use EXISTS instead of IN. NOT IN can be particularly slow. You
don't need the DISTINCT's either, I think.

SELECT last_name FROM personal_data WHERE EXISTS (SELECT id FROM
event_detail,event_universe WHERE id=personal_data.id AND
event_detail.eventtypeid=event_universe.eventtypeid AND
event_universe.studentstatus LIKE 'Student' AND
event_detail.date_effective<=datetime(date('1-jan-1998')))
AND NOT EXISTS
(SELECT id FROM event_detail,event_universe WHERE id=personal_data.id AND
event_detail.eventtypeid=event_universe.eventtypeid AND
event_universe.studentstatus LIKE 'NotStudent' AND
event_detail.date_effective<=datetime(date('1-jun-1998')));

Should be right...
M.




view thread (2+ messages)

Message-ID: <007d01bfa151$0a23bb40$01c8a8c0@malthouse.private.net>
Permalink:  ../007d01bfa151$0a23bb40$01c8a8c0@malthouse.private.net/
Also on:    postgresql.org/message-id/007d01bfa151$0a23bb40$01c8a8c0@malthouse.private.net

 · 

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: moray.mcconnachie@computing-services.oxford.ac.uk, aperrin@demog.berkeley.edu
  Subject: Re: Speeding up a query
  In-Reply-To: <007d01bfa151$0a23bb40$01c8a8c0@malthouse.private.net>
  References: <Pine.LNX.4.10.10004071209290.7254-100000@famine.DEMOG.Berkeley.EDU> <007d01bfa151$0a23bb40$01c8a8c0@malthouse.private.net>

* 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