Speeding up a query
От | Andrew Perrin - Demography |
---|---|
Тема | Speeding up a query |
Дата | |
Msg-id | Pine.LNX.4.10.10004071209290.7254-100000@famine.DEMOG.Berkeley.EDU обсуждение исходный текст |
Список | 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@demog.berkeley.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
В списке pgsql-sql по дате отправления: