Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id F0888B5DBD1 for ; Thu, 22 Sep 2011 22:32:10 -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 37552-04 for ; Fri, 23 Sep 2011 01:32:02 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.8.0-rc2 Received: from mail-yx0-f174.google.com (mail-yx0-f174.google.com [209.85.213.174]) by mail.postgresql.org (Postfix) with ESMTP id A13B4B5DBE0 for ; Thu, 22 Sep 2011 22:32:02 -0300 (ADT) Received: by yxl31 with SMTP id 31so2382183yxl.19 for ; Thu, 22 Sep 2011 18:32:02 -0700 (PDT) Received: by 10.236.131.106 with SMTP id l70mr18257891yhi.29.1316741522712; Thu, 22 Sep 2011 18:32:02 -0700 (PDT) Received: from [153.65.52.55] (71-6-94-66.static-ip.telepacific.net. [71.6.94.66]) by mx.google.com with ESMTPS id d5sm10828517yhl.19.2011.09.22.18.32.00 (version=SSLv3 cipher=OTHER); Thu, 22 Sep 2011 18:32:01 -0700 (PDT) Subject: Re: IN or EXISTS From: Jeff Davis To: Craig Ringer Cc: Andy Colson , "pgsql-performance@postgresql.org" In-Reply-To: <4E5D8F68.4010102@ringerc.id.au> References: <4E5D485F.8060300@squeakycode.net> <4E5D8F68.4010102@ringerc.id.au> Content-Type: text/plain; charset="ISO-8859-1" Date: Thu, 22 Sep 2011 18:32:00 -0700 Message-ID: <1316741520.31091.7.camel@sussancws0025> Mime-Version: 1.0 X-Mailer: Evolution 2.28.3 Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.9 tagged_above=-10 required=5 tests=BAYES_00=-1.9 X-Spam-Level: X-Archive-Number: 201109/352 X-Sequence-Number: 45128 On Wed, 2011-08-31 at 09:33 +0800, Craig Ringer wrote: > On the other hand, the `IN' subquery is uncorrelated needs only run > once, where the `EXISTS' subquery is correlated and has to run once for > every outer record. If the EXISTS looks semantically similar to an IN (aside from NULL semantics), then it can be made into a semijoin. It doesn't require re-executing any part of the plan. I don't think there are any cases where [NOT] IN is an improvement, am I mistaken? > Another complication is the possible presence of NULL in an IN list. > Getting NULLs in `IN' lists is a common source of questions on this > list, because people are quite surprised by how it works. EXISTS avoids > the NULL handling issue (and in the process demonstrates how woefully > inconsistent SQL's handling of NULL really is). Absolutely. The NULL behavior of IN is what makes it hard to optimize, and therefore you should use EXISTS instead if the semantics are suitable. > Theoretically the query planner could transform: > > SELECT * from y WHERE y.id IN (SELECT DISTINCT z.y_id FROM z WHERE > z.y_id IS NOT NULL); > > into: > > SELECT * FROM y WHERE EXISTS (SELECT 1 FROM z WHERE z.y_id = y.id) > > ... or vice versa depending on which it thought would be faster. Although those two queries are semantically the same (I think), a lot of very similar pairs of queries are not equivalent. For instance, if it was a NOT IN you couldn't change that to a NOT EXISTS. Regards, Jeff Davis