Re: negative queries puzzle - Mailing list pgsql-sql

From Christopher Kings-Lynne
Subject Re: negative queries puzzle
Date
Msg-id GNELIHDDFBOCMGBFGEFOEEHFCDAA.chriskl@familyhealth.com.au
Whole thread Raw
In response to negative queries puzzle  (Jinn Koriech <lists@idealint.co.uk>)
List pgsql-sql
> but then to get the entirely new items out i use a sub query which takes
> for ever
>
> SELECT DISTINCT * FROM v_postcode_new WHERE postcode NOT IN ( SELECT
> postcode FROM v_postcode_old ) ORDER BY postcode ASC;

NOT IN is known to be very, very slow in Postgres.  Use NOT EXISTS instead:

SELECT DISTINCT * FROM v_postcode_new vpn WHERE NOT EXISTS (SELECT TRUE FROM
v_postcode_old vpo WHERE vpo.postcode=vpn.postcode) ORDER BY postcode ASC;

Chris



pgsql-sql by date:

Previous
From: Stephan Szabo
Date:
Subject: Re: FW: Case Statement
Next
From: Ludwig Lim
Date:
Subject: Re: negative queries puzzle