Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1T6k4a-0003no-Kz for pgsql-bugs@postgresql.org; Wed, 29 Aug 2012 15:20:52 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1T6k4V-0004Zq-Bg for pgsql-bugs@postgresql.org; Wed, 29 Aug 2012 15:20:52 +0000 Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.5/8.14.5) with ESMTP id q7TFKjXm010471; Wed, 29 Aug 2012 11:20:45 -0400 (EDT) From: Tom Lane To: stefan@konink.de cc: pgsql-bugs@postgresql.org Subject: Re: BUG #7509: x NOT IN (select x from z) extremely slow in compare to select x from y except select x from z; In-reply-to: References: Comments: In-reply-to stefan@konink.de message dated "Wed, 29 Aug 2012 10:31:27 -0000" Date: Wed, 29 Aug 2012 11:20:44 -0400 Message-ID: <10470.1346253644@sss.pgh.pa.us> X-Pg-Spam-Score: -2.1 (--) X-Archive-Number: 201208/223 X-Sequence-Number: 33947 stefan@konink.de writes: > The following is relatively fast: > bag-2012-aug=# explain select count(*) from (select kvk from kvk_normal > except select kvk from bag_kvk) as x; > The 'normal' case basically doesn't finish: > bag-2012-aug=# explain select count(*) from (select kvk_normal.kvk from > kvk_normal where kvk_normal.kvk not in (select bag_kvk.kvk from bag_kvk)) as > x; NOT IN is difficult to optimize, as well as hard to use, because of its rather bizarre behavior for nulls. You might consider using NOT EXISTS instead. regards, tom lane