Re: slow query - where not in - Mailing list pgsql-performance

From Michael Paesold
Subject Re: slow query - where not in
Date
Msg-id 028e01c2f54a$9af3d010$3201a8c0@beeblebrox
Whole thread Raw
In response to slow query - where not in  (Jeremiah Elliott <jelliott@artcobell.com>)
Responses Re: slow query - where not in
List pgsql-performance
Bruno Wolff III <bruno@wolff.to> wrote:

> Jeremiah Elliott <jelliott@artcobell.com> wrote:
> > here is the query that is killing me:
> >
> > select shoporder from sodetailtabletrans where shoporder not in(select
> > shoporder from soheadertable)
>

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

I think this should rather be:

SELECT shoporder FROM sodetailtabletrans
  WHERE NOT EXISTS (
    SELECT 1 FROM soheadertable
    WHERE soheadertable.shoporder = sodetailtabletrans.shoporder
    )

Regards,
Michael Paesold


pgsql-performance by date:

Previous
From: Greg Stark
Date:
Subject: Re: slow query - where not in
Next
From: Ulli Mueckstein
Date:
Subject: calling analyze from a stored procedure in C