X-Original-To: pgsql-sql@postgresql.org Received: from fzkmail2.fzk.de (fzkmail2.fzk.de [141.52.27.52]) by postgresql.org (Postfix) with SMTP id 473B4475458 for ; Tue, 18 Mar 2003 08:47:48 -0500 (EST) Received: FROM resy5.fzk.de BY fzkmail2.fzk.de ; Tue Mar 18 14:47:49 2003 +0100 Received: œby rodos.fzk.de with ESMTP (8.8.6 (PHNE_17135)/8.7.1) id OAA08147; Tue, 18 Mar 2003 14:47:49 +0100 (MET) Message-ID: <3E772384.A397FE7D@rodos.fzk.de> Date: Tue, 18 Mar 2003 14:47:48 +0100 From: Christoph Haller X-Mailer: Mozilla 4.77 [en] (X11; U; HP-UX B.10.20 9000/780) X-Accept-Language: en MIME-Version: 1.0 To: pgsql-sql@postgresql.org Cc: datactrl@tpg.com.au Subject: Re: the best way to get some records not in another table Content-Type: text/plain; charset=iso-8859-2 Content-Transfer-Encoding: 7bit X-Archive-Number: 200303/432 X-Sequence-Number: 12471 > > 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