Re: the best way to get some records not in another table

From: Christoph Haller <ch(at)rodos(dot)fzk(dot)de>
To: pgsql-sql(at)postgresql(dot)org
Cc: datactrl(at)tpg(dot)com(dot)au
Subject: Re: the best way to get some records not in another table
Date: 2003-03-18 13:47:48
Message-ID: 3E772384.A397FE7D@rodos.fzk.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

>
> Try to get some records not in another table. As the following, please

> advise which one will be the best way to do. Or is there any other way
to do
> better?
>
> SELECT DISTINCT a.c1
> FROM test_j2 a
> WHERE a.c1 NOT IN (SELECT DISTINCT b.c1 FROM test_j1 b);
>
> SELECT a.c1 FROM test_j2 a
> EXCEPT
> SELECT b.c1 FROM test_j1 b;
>
IN resp. NOT IN clauses are known to be slow.

SELECT DISTINCT a.c1
FROM test_j2 a
WHERE NOT EXISTS
(SELECT b.c1 FROM test_j1 b WHERE b.c1 = a.c1) ;

Can't tell if EXISTS performs better than EXCEPT,
have a look at the EXPLAIN output.

Regards, Christoph

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Miguel Carvalho 2003-03-18 14:15:31 Trigger issue, bug? on 7.2.1
Previous Message Miguel Carvalho 2003-03-18 13:46:36 Trigger issue, bug? on 7.2.1