Received: from makus.postgresql.org ([98.129.198.125]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TEYYJ-00010I-O3 for pgsql-bugs@postgresql.org; Thu, 20 Sep 2012 04:39:51 +0000 Received: from mail-pb0-f46.google.com ([209.85.160.46]) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TEYYF-00052j-Ep for pgsql-bugs@postgresql.org; Thu, 20 Sep 2012 04:39:49 +0000 Received: by pbbrr13 with SMTP id rr13so4155392pbb.19 for ; Wed, 19 Sep 2012 21:39:45 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=message-id:date:from:user-agent:mime-version:to:cc:subject :references:in-reply-to:content-type:content-transfer-encoding :x-gm-message-state; bh=581JFQgM0GGsBviGBZmJwSfTauzgYSBuzLnjT3aJWYI=; b=kIUn4BES86JCABWnnrTPH69pnIt84q7kUoEJFC08DpJ7kg/9XWUGdrcWTLn0xQOM85 FikLJFdJnpM3iLXlaoHPTRJCPnXkTwgdvlqwy3bbO25Z0WOQUnGBynvSyT+idYcKAP/k +EKtSOKLYj6bGXvYf7OyauO/EYmFOze3JGbcIoG9g8Xa9WJDFEWhN1R83WwmGc7pD/tB rgTsHtdbhoFkyShUV9g/c2xIBMzhQvAgDWeomgfTJle8ypk5vgk5wRCEi10ZZYunSSL1 4LZOSkRvQMBg5VxXUUwy57USI++hQD6l3s9ra0hldweQ9fL4Hh9KDQu36fTfneNyCP/l TuNw== Received: by 10.68.223.4 with SMTP id qq4mr3656870pbc.10.1348115984888; Wed, 19 Sep 2012 21:39:44 -0700 (PDT) Received: from ayaki.localdomain (office1.postnewspapers.com.au. [150.101.171.128]) by mx.google.com with ESMTPS id sz4sm2907958pbc.22.2012.09.19.21.39.41 (version=SSLv3 cipher=OTHER); Wed, 19 Sep 2012 21:39:43 -0700 (PDT) Message-ID: <505A9E0B.3000600@ringerc.id.au> Date: Thu, 20 Sep 2012 12:39:39 +0800 From: Craig Ringer User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:14.0) Gecko/20120717 Thunderbird/14.0 MIME-Version: 1.0 To: l1t@tom.com CC: pgsql-bugs@postgresql.org Subject: Re: BUG #7556: "select not in sub query" plan very poor vs "not exists" References: In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Gm-Message-State: ALoCoQmv22VQb2pSFIc0QcUxxyuU5Xr0cSa8Ff3r/zSyHbdNccBJJ3g+mb4jcLSDLMSLRuAVYNWS X-Pg-Spam-Score: -1.9 (-) X-Archive-Number: 201209/223 X-Sequence-Number: 34217 On 09/19/2012 01:48 PM, l1t@tom.com wrote: > The following bug has been logged on the website: > > Bug reference: 7556 > Logged by: lt > Email address: l1t@tom.com > PostgreSQL version: 9.2.0 > Operating system: windows xp > Description: > create table sli_test (id int primary key,info varchar(20)); insert into sli_test select generate_series(1,1000000),'digoal'||generate_series(1,1000000); analyze verbose sli_test; create table sli_test2 (id int not null,info varchar(20)); insert into sli_test2 select generate_series(1,1000000),'dbase'||generate_series(1,1000000); analyze verbose sli_test2; explain select max(a.info)from sli_test a where a.id not in(select b.id from sli_test2 b where b.id<50000); > QUERY PLAN > --------------------------------------------------------------------------------------- > Aggregate (cost=9241443774.00..9241443774.01 rows=1 width=12) Here's what I get on 9.1: regress=# explain select max(a.info)from sli_test a where a.id not in(select regress(# b.id from sli_test2 b where b.id<50000); QUERY PLAN --------------------------------------------------------------------------------- Aggregate (cost=38050.82..38050.83 rows=1 width=12) -> Seq Scan on sli_test a (cost=18026.82..36800.82 rows=500000 width=12) Filter: (NOT (hashed SubPlan 1)) SubPlan 1 -> Seq Scan on sli_test2 b (cost=0.00..17906.00 rows=48329 width=4) Filter: (id < 50000) (6 rows) It runs in about 500ms here. You don't appear to have posted the full query plan, so it's hard to compare. In general, `NOT IN` is a poor formulation for a query; you're better off with a JOIN or with `NOT EXISTS`. See eg http://stackoverflow.com/questions/12444142/postgresql-how-to-figure-out-missing-numbers-in-a-column-using-generate-series/12444165#12444165 -- Craig Ringer