Received: from makus.postgresql.org (makus.postgresql.org [98.129.198.125]) by mail.postgresql.org (Postfix) with ESMTP id 76C1AEC7B2A for ; Fri, 6 Jul 2012 21:40:00 -0300 (ADT) Received: from mail-pb0-f46.google.com ([209.85.160.46]) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1SnJ42-0004q9-VW for pgsql-general@postgresql.org; Sat, 07 Jul 2012 00:40:00 +0000 Received: by pbbrp8 with SMTP id rp8so15438228pbb.19 for ; Fri, 06 Jul 2012 17:39:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=message-id:date:from:user-agent:mime-version:to:subject:references :in-reply-to:content-type:content-transfer-encoding; bh=Ylo8MqJLyFF6JTzzSOwM4U0qnWYrbzE+7o9Dg+O7KeQ=; b=FB2TWKPAWe+WBC3L+9PJniWrlXTBndlr20F4QdyPdKZOS30T1QVZj8RltRcQtHSVfw r7nQKpLsCb/v4uZ9Rhy+igrQbETIW8YX/zpVDW4HKUhS1cDD5sqShLW3/Y2ULKru0QRM zyJtCMPtND91mMdWKJ7q0EAdHLv3q2E6sxwI/BIoH+tIkFBStNwT//pgCCDdTcMCh66b Kf1Dj20GgHw5g7DWWppU5id/+4dh7VkGfRc6M39kkmRLyu1u2h0ZixLzxLpyTcq+8xvq M5w2+CFTMK/wF32vTCol7/CJDsKOlMlDWp8lcDsAkSBCysAxMS22N0pGislKzTDeuEp9 2DnA== Received: by 10.68.135.201 with SMTP id pu9mr41017213pbb.146.1341621585846; Fri, 06 Jul 2012 17:39:45 -0700 (PDT) Received: from [172.16.1.232] ([208.187.158.94]) by mx.google.com with ESMTPS id nj4sm8887240pbc.5.2012.07.06.17.39.44 (version=SSLv3 cipher=OTHER); Fri, 06 Jul 2012 17:39:45 -0700 (PDT) Message-ID: <4FF78634.9080800@gmail.com> Date: Fri, 06 Jul 2012 18:43:32 -0600 From: Rob Sargent User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:13.0) Gecko/20120615 Thunderbird/13.0.1 MIME-Version: 1.0 To: pgsql-general@postgresql.org Subject: Re: Help with sql References: <06772287-B2D0-4C9C-826C-7D49E1015978@gmail.com> In-Reply-To: <06772287-B2D0-4C9C-826C-7D49E1015978@gmail.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: -2.6 (--) X-Archive-Number: 201207/118 X-Sequence-Number: 188629 On 07/06/2012 03:34 PM, Perry Smith wrote: > Hi Guys, > > This isn't a PostgreSQL specific question but just a SQL question. If this is not an appropriate question for this list, please let me know. > > It is also, perhaps, a really silly question. > > This query (without the 'explain' keyword) , when executed takes forever and a day: > >> condor_development=> explain select id from filesets where id not in ( select fileset_id from service_pack_fileset_maps ); >> QUERY PLAN >> ---------------------------------------------------------------------------------------------- >> Seq Scan on filesets (cost=0.00..71937742.00 rows=26088 width=4) >> Filter: (NOT (SubPlan 1)) >> SubPlan 1 >> -> Materialize (cost=0.00..2517.78 rows=95852 width=4) >> -> Seq Scan on service_pack_fileset_maps (cost=0.00..1663.52 rows=95852 width=4) >> (5 rows) > > This query returns within a second: > >> condor_development=> explain select id from filesets where id not in ( select distinct fileset_id from service_pack_fileset_maps ); >> QUERY PLAN >> ---------------------------------------------------------------------------------------------- >> Seq Scan on filesets (cost=2102.31..3153.53 rows=26088 width=4) >> Filter: (NOT (hashed SubPlan 1)) >> SubPlan 1 >> -> HashAggregate (cost=1903.15..2062.48 rows=15933 width=4) >> -> Seq Scan on service_pack_fileset_maps (cost=0.00..1663.52 rows=95852 width=4) >> (5 rows) > > The difference is the "distinct" keyword in the inner select. > > What I'm confused about is why isn't the "distinct" implicit? I thought the construct "blah in ( select ... )" was using "sets" and an item (I thought) can not be in a set more than once. > > Perhaps my question is the opposite really? Why would you not always use "distinct" in the inner select when the operator is "in" or "not in" ? > > And if I can throw in another question on top: is there a different method other than "not in" that would work better? > > Thank you guys for the help and a really awesome database. > pedz > (this is *suppose* to be sent as plain text... I hope my mailer does what it is told) > > Well they are distinct records, they just may have the same values. And I'm not trying to be flippant. We don't see the structure of those table: are all the id fields involved primary keys or with unique index coverage? Does "not exists ( select fileset.id = fileset_id from service_pack_fileset_map) change the behaviour?