Re: slow query - where not in

From: Bruno Wolff III <bruno(at)wolff(dot)to>
To: Jeremiah Elliott <jelliott(at)artcobell(dot)com>
Cc: pgsql-performance(at)postgresql(dot)org
Subject: Re: slow query - where not in
Date: 2003-03-28 15:59:33
Message-ID: 20030328155933.GA10761@wolff.to
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

On Fri, Mar 28, 2003 at 09:38:50 -0600,
Jeremiah Elliott <jelliott(at)artcobell(dot)com> wrote:
> here is the query that is killing me:
>
> select shoporder from sodetailtabletrans where shoporder not in(select
> shoporder from soheadertable)

This will probably work better in 7.4.

For now there are several ways to rewrite this query.

If there are no null values for shoporder in soheadertable or
sodetailtabletrans you can use not exists instead of not in:
select shoporder from sodetailtabletrans where shoporder not exists(select
shoporder from soheadertable)

You can use set difference to calculate the result:
select shoporder from sodetailtabletrans except all select
shoporder from soheadertable

If there are no null values for shoporder in one of sodetailtabletrans
or soheadertable you can user an outer join with a restriction that limits
the rows of interest to those that don't match:
select sodetailtabletrans.shoporder from sodetailtabletrans left join
soheadertable using (shoporder) where soheadertable.shoporder is null

In response to

Responses

Browse pgsql-performance by date

  From Date Subject
Next Message Greg Stark 2003-03-28 16:20:29 Re: slow query - where not in
Previous Message Jeremiah Elliott 2003-03-28 15:38:50 slow query - where not in