Speeding up a query

From: Andrew Perrin - Demography <aperrin(at)demog(dot)berkeley(dot)edu>
To: pgsql-sql(at)postgresql(dot)org
Subject: Speeding up a query
Date: 2000-04-07 19:12:39
Message-ID: Pine.LNX.4.10.10004071209290.7254-100000@famine.DEMOG.Berkeley.EDU
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Greetings.

The following query returns the last names of people in our database whom
we consider 'students' during the time period 1-jan-1998 to 1-jun-1998.
It's based on selecting events from a table in which each event can
determine that a person is a Student, NotStudent, or Neutral; the idea is
that a given person is a student from the date of their first Student
event to the date of their next NotStudent event, etc.

The problem is that it's very slow - on the order of 45-60 seconds to
generate a list that's only about 30 names long. Can anyone offer some
advice on speeding it up? The dates need to be user-selectable.

Many thanks.

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')))

---------------------------------------------------------------------
Andrew J. Perrin - aperrin(at)demog(dot)berkeley(dot)edu - NT/Unix Admin/Support
Department of Demography - University of California at Berkeley
2232 Piedmont Avenue #2120 - Berkeley, California, 94720-2120 USA
http://demog.berkeley.edu/~aperrin --------------------------SEIU1199

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Justin Long 2000-04-07 19:46:04 SQL syntax for updating tables
Previous Message Andy Lewis 2000-04-07 17:46:42 Cursors and PHP