Received: from localhost (unknown [200.46.204.183]) by postgresql.org (Postfix) with ESMTP id 1C3DF64FD33 for ; Thu, 31 Jul 2008 00:13:21 -0300 (ADT) Received: from postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 68550-09-2 for ; Thu, 31 Jul 2008 00:13:05 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by postgresql.org (Postfix) with ESMTP id 9CBD964FD05 for ; Thu, 31 Jul 2008 00:08:08 -0300 (ADT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.2/8.14.2) with ESMTP id m6V386gE027821; Wed, 30 Jul 2008 23:08:06 -0400 (EDT) To: Miernik cc: pgsql-performance@postgresql.org Subject: Re: why "WHERE uid NOT IN" is so slow, and EXCEPT in the same situtation is fast? In-reply-to: <20080731024508.53F6.0.NOFFLE@turbacz.local> References: <20080731024508.53F6.0.NOFFLE@turbacz.local> Comments: In-reply-to Miernik message dated "Thu, 31 Jul 2008 04:45:08 +0200" Date: Wed, 30 Jul 2008 23:08:06 -0400 Message-ID: <27820.1217473686@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0 tagged_above=0 required=5 tests=none X-Spam-Level: X-Archive-Number: 200807/265 X-Sequence-Number: 30865 Miernik writes: > Two queries which do the same thing, first one takes ages to complete > (did wait several minutes and cancelled it), while the second one took > 9 seconds? Don't they do the same thing? Hmm, what have you got work_mem set to? The first one would likely have been a lot faster if it had hashed the subplan; which I'd have thought would happen with only 80K rows in the subplan result, except it didn't. The queries are in fact not exactly equivalent, because EXCEPT involves some duplicate-elimination behavior that won't happen in the NOT IN formulation. So I don't apologize for your having gotten different plans. But you should have gotten a plan less awful than that one for the NOT IN. Another issue is that the NOT IN will probably not do what you expected if the subquery yields any NULLs. regards, tom lane