Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1T6fYb-0008DJ-Fu for pgsql-bugs@postgresql.org; Wed, 29 Aug 2012 10:31:33 +0000 Received: from mahout.postgresql.org ([98.129.198.123]) by magus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1T6fYY-0000MF-1N for pgsql-bugs@postgresql.org; Wed, 29 Aug 2012 10:31:33 +0000 Received: from [204.145.124.246] (helo=wrigleys.postgresql.org) by mahout.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1T6fYW-0007II-A0 for pgsql-bugs@postgresql.org; Wed, 29 Aug 2012 10:31:29 +0000 Received: from www-data by wrigleys.postgresql.org with local (Exim 4.72) (envelope-from ) id 1T6fYV-0001eM-S3 for pgsql-bugs@postgresql.org; Wed, 29 Aug 2012 10:31:27 +0000 Date: Wed, 29 Aug 2012 10:31:27 +0000 Message-Id: Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Subject: BUG #7509: x NOT IN (select x from z) extremely slow in compare to select x from y except select x from z; To: pgsql-bugs@postgresql.org From: stefan@konink.de X-Pg-Spam-Score: -2.1 (--) X-Archive-Number: 201208/220 X-Sequence-Number: 33944 The following bug has been logged on the website: Bug reference: 7509 Logged by: Stefan de Konink Email address: stefan@konink.de PostgreSQL version: 9.1.5 Operating system: Linux = Description: = The following is relatively fast: bag-2012-aug=3D# explain select count(*) from (select kvk from kvk_normal except select kvk from bag_kvk) as x; QUERY PLAN = = ---------------------------------------------------------------------------= ------------------------------- Aggregate (cost=3D1110465.88..1110465.89 rows=3D1 width=3D0) -> Subquery Scan on x (cost=3D1042163.45..1102413.23 rows=3D3221060 width=3D0) -> SetOp Except (cost=3D1042163.45..1070202.63 rows=3D3221060 width=3D8) -> Sort (cost=3D1042163.45..1056183.04 rows=3D5607836 widt= h=3D8) Sort Key: "*SELECT* 1".kvk -> Append (cost=3D0.00..183539.72 rows=3D5607836 width=3D8) -> Subquery Scan on "*SELECT* 1" = (cost=3D0.00..122902.20 rows=3D3221060 width=3D8) -> Seq Scan on kvk_normal = (cost=3D0.00..90691.60 rows=3D3221060 width=3D8) -> Subquery Scan on "*SELECT* 2" = (cost=3D0.00..60637.52 rows=3D2386776 width=3D8) -> Seq Scan on bag_kvk = (cost=3D0.00..36769.76 rows=3D2386776 width=3D8) The 'normal' case basically doesn't finish: bag-2012-aug=3D# 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; QUERY PLAN = = ---------------------------------------------------------------------------= ---------- Aggregate (cost=3D103065293697.97..103065293697.98 rows=3D1 width=3D0) -> Seq Scan on kvk_normal (cost=3D0.00..103065289671.65 rows=3D1610530 width=3D0) Filter: (NOT (SubPlan 1)) SubPlan 1 -> Materialize (cost=3D0.00..58027.64 rows=3D2386776 width=3D8) -> Seq Scan on bag_kvk (cost=3D0.00..36769.76 rows=3D238= 6776 width=3D8) (6 rows) Table size is 3.2mil rows in adres, and 2.3mil rows in bag_kvk.