Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id EF6691337990 for ; Sun, 31 Oct 2010 11:10:57 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.29]) (amavisd-maia, port 10024) with ESMTP id 70715-10 for ; Sun, 31 Oct 2010 14:10:52 +0000 (UTC) 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 mail.postgresql.org (Postfix) with ESMTP id 5BCAA1336705 for ; Sun, 31 Oct 2010 11:10:50 -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 o9VEAlYY019770; Sun, 31 Oct 2010 10:10:48 -0400 (EDT) To: James Cloos cc: pgsql-sql@postgresql.org Subject: Re: A more efficient way? In-reply-to: References: Comments: In-reply-to James Cloos message dated "Sun, 31 Oct 2010 02:59:20 -0400" Date: Sun, 31 Oct 2010 10:10:47 -0400 Message-ID: <19769.1288534247@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.91 tagged_above=-5 required=5 tests=BAYES_00=-1.9, T_RP_MATCHES_RCVD=-0.01 X-Spam-Level: X-Archive-Number: 201010/80 X-Sequence-Number: 34731 James Cloos writes: > I've a third-party app which is slower than it ought to be. > It does one operation in particular by doing a query to get a list of > rows, and then iterates though them to confirm whether it actually wants > that row. As you can imagine that is very slow. > This query gets the correct data set in one go, but is also slow: > select p from m where s = 7 and p not in (select p from m where s != 7); See if you can recast it as a NOT EXISTS. NOT IN is hard to optimize because of its weird behavior with nulls. If you're working with a less-than-current version of PG, you may instead have to resort to a left-join-with-is-null locution, ie select m.p from m left join (select p from m where s != 7) m2 on (m.p=m2.p) where m2.p is null and m.s = 7; but it's hard to wrap your head around that sort of thing, so I'd advise against using it if you can get decent performance with EXISTS. regards, tom lane