Received: from localhost (unknown [200.46.204.183]) by postgresql.org (Postfix) with ESMTP id E5EA464FCB8 for ; Thu, 31 Jul 2008 00:55:31 -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 05849-06 for ; Thu, 31 Jul 2008 00:55:12 -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 DDFE164FCBF for ; Thu, 31 Jul 2008 00:55: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 m6V3t7Su028449; Wed, 30 Jul 2008 23:55:08 -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: <20080731031822.GA813@tarnica> References: <20080731024508.53F6.0.NOFFLE@turbacz.local> <27820.1217473686@sss.pgh.pa.us> <20080731031822.GA813@tarnica> Comments: In-reply-to Miernik message dated "Thu, 31 Jul 2008 05:18:22 +0200" Date: Wed, 30 Jul 2008 23:55:07 -0400 Message-ID: <28448.1217476507@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/267 X-Sequence-Number: 30867 Miernik writes: > On Wed, Jul 30, 2008 at 11:08:06PM -0400, Tom Lane wrote: >> 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. > work_mem = 1024kB Try increasing that ... I don't recall the exact per-row overhead but I'm quite sure it's more than 8 bytes. Ten times that would likely get you to a hash subquery plan. > The machine has 48 MB total RAM and is a Xen host. 48MB is really not a sane amount of memory to run a modern database in. Maybe you could make it go with sqlite or some other tiny-footprint DBMS, but Postgres isn't focused on that case. >> 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 if use EXCEPT ALL? Fraid not, EXCEPT ALL has yet other rules for how it deals with duplicates. >> Another issue is that the NOT IN will probably not do what you >> expected if the subquery yields any NULLs. > In this specific query I think it is not possible for the subquery to > have NULLs, Okay, just wanted to point out a common gotcha. regards, tom lane