Received: from magus.postgresql.org (magus.postgresql.org [87.238.57.229]) by mail.postgresql.org (Postfix) with ESMTP id F016A7B4D3F for ; Sat, 7 Jul 2012 10:48:28 -0300 (ADT) Received: from mail-ob0-f174.google.com ([209.85.214.174]) by magus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1SnVN1-0001xi-DR for pgsql-general@postgresql.org; Sat, 07 Jul 2012 13:48:27 +0000 Received: by obbuo13 with SMTP id uo13so16848961obb.19 for ; Sat, 07 Jul 2012 06:48:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=content-type:mime-version:subject:from:in-reply-to:date :content-transfer-encoding:message-id:references:to:x-mailer; bh=/rQhzygvJPwNW2c6v3y8RNTLfazKyyYaWiIun2MExpo=; b=MmzA0ENznl+uguYjWKaihfrfgrA0eSkoJjsecX47or8H5Lf1CnZlaoeQ45I8riTox7 S9vngbufNoucF+BtMGIJTrRcIi8mfFoqIztTtQUS5yyMSbyo8amxmOqYUl7rzBwmPVYz tYTytxNjsdgHuxifRIlsAt9m39i4ig2xLlYXG2AFOzfKcHqFkRi1/W+RL1ehGE/6g1Wm Il+O4H4uR3G3mUynm4oAkW0aFOltSaa/24uzoQelD63SI0S+5ZEHafvf9yUGwfhsEPsp qlrVcC3fSV2yK6UvGb3RwicqPpnhWeIdqSlw0hh32hSb+IjgP7pADb4Mr9c33316l/EQ ssUQ== Received: by 10.182.192.41 with SMTP id hd9mr29589872obc.17.1341668889950; Sat, 07 Jul 2012 06:48:09 -0700 (PDT) Received: from [10.0.0.3] ([64.128.19.234]) by mx.google.com with ESMTPS id c6sm25793513obl.22.2012.07.07.06.48.07 (version=TLSv1/SSLv3 cipher=OTHER); Sat, 07 Jul 2012 06:48:09 -0700 (PDT) Content-Type: text/plain; charset=iso-8859-1 Mime-Version: 1.0 (Apple Message framework v1257) Subject: Re: Help with sql From: Perry Smith In-Reply-To: <4FF78947.5080902@pinpointresearch.com> Date: Sat, 7 Jul 2012 08:48:06 -0500 Content-Transfer-Encoding: quoted-printable Message-Id: <1E64A6FD-FF31-48DE-B1BA-0C50F7AB4521@gmail.com> References: <06772287-B2D0-4C9C-826C-7D49E1015978@gmail.com> <4FF78947.5080902@pinpointresearch.com> To: "" X-Mailer: Apple Mail (2.1257) X-Pg-Spam-Score: -2.7 (--) X-Archive-Number: 201207/127 X-Sequence-Number: 188638 On Jul 6, 2012, at 7:56 PM, Steve Crawford wrote: > On 07/06/2012 02:34 PM, Perry Smith wrote: >> Hi Guys, >>=20 >> 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. >>=20 >> It is also, perhaps, a really silly question. >>=20 >> This query (without the 'explain' keyword) , when executed takes = forever and a day: >>=20 >>> condor_development=3D> explain select id from filesets where id not = in ( select fileset_id from service_pack_fileset_maps ); >>> QUERY PLAN >>> = --------------------------------------------------------------------------= -------------------- >>> Seq Scan on filesets (cost=3D0.00..71937742.00 rows=3D26088 = width=3D4) >>> Filter: (NOT (SubPlan 1)) >>> SubPlan 1 >>> -> Materialize (cost=3D0.00..2517.78 rows=3D95852 width=3D4) >>> -> Seq Scan on service_pack_fileset_maps = (cost=3D0.00..1663.52 rows=3D95852 width=3D4) >>> (5 rows) >> This query returns within a second: >>=20 >>> condor_development=3D> 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=3D2102.31..3153.53 rows=3D26088 = width=3D4) >>> Filter: (NOT (hashed SubPlan 1)) >>> SubPlan 1 >>> -> HashAggregate (cost=3D1903.15..2062.48 rows=3D15933 = width=3D4) >>> -> Seq Scan on service_pack_fileset_maps = (cost=3D0.00..1663.52 rows=3D95852 width=3D4) >>> (5 rows) >> The difference is the "distinct" keyword in the inner select. >>=20 >> 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. >>=20 >> 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" = ? >>=20 >> And if I can throw in another question on top: is there a different = method other than "not in" that would work better? > Actually it is *very* PostgreSQL specific. In fact, it may even be = PostgreSQL *version* specific as you are delving into how the planner = decides how to handle a query. >=20 > It appears that the planner is assuming, based on collected stats and = available indexes, that there will be roughly 1/6 the records returned = by the "distinct" query and thus chose a different method to join the = records. One useful piece of information would be the indexes on the two = tables. >=20 > As to other methods, you can use: > ... where not exists (select 1 from service_pack_fileset_maps where = fileset_id =3D filesets.id)... > (Note: as alluded to above, ...not in... works better in some releases = and ...not exists... better in others due to improvements over time.) >=20 > Still another method: > select id from filesets except select fileset_id from = service_pack_fileset_maps; Thanks guys. Small side note: I thought I saw "set difference" in the documentation = but I couldn't find it. It appears "EXCEPT" is set difference. Thank = you for that tidbit. The database is mostly static. I run through a very lengthy process to = populate the database maybe once a month and then it is 99% read-only. = By far, most of the accesses are via a view that I have that is rather = long and ugly so I won't paste it in. I've tried to make this = particular view as fast as possible so the indexes, etc are what I think = will help that out. The version is psql (PostgreSQL) 9.0.4 Each table has a key of "id" -- this database was created by / used by = Ruby on Rails and that is how it likes to do things. > condor_development=3D> \d service_pack_fileset_maps > Table = "public.service_pack_fileset_maps" > Column | Type | = Modifiers =20 > = -----------------+-----------------------------+--------------------------= ---------------------------------------------- > id | integer | not null default = nextval('service_pack_fileset_maps_id_seq'::regclass) > service_pack_id | integer | not null > fileset_id | integer | not null > created_at | timestamp without time zone |=20 > updated_at | timestamp without time zone |=20 > Indexes: > "service_pack_fileset_maps_pkey" PRIMARY KEY, btree (id) > "service_pack_fileset_maps_service_pack_id_key" UNIQUE, btree = (service_pack_id, fileset_id) > "index_service_pack_fileset_maps_on_fileset_id" btree (fileset_id) > Foreign-key constraints: > "service_pack_fileset_maps_fileset_id_fkey" FOREIGN KEY = (fileset_id) REFERENCES filesets(id) ON DELETE CASCADE DEFERRABLE > "service_pack_fileset_maps_service_pack_id_fkey" FOREIGN KEY = (service_pack_id) REFERENCES service_packs(id) ON DELETE CASCADE = DEFERRABLE > condor_development=3D> \d filesets > Table "public.filesets" > Column | Type | = Modifiers =20 > = ------------+-----------------------------+-------------------------------= ------------------------ > id | integer | not null default = nextval('filesets_id_seq'::regclass) > lpp_id | integer | not null > vrmf | character varying(255) | not null > created_at | timestamp without time zone |=20 > updated_at | timestamp without time zone |=20 > Indexes: > "filesets_pkey" PRIMARY KEY, btree (id) > "filesets_lpp_id_key" UNIQUE, btree (lpp_id, vrmf) > Foreign-key constraints: > "filesets_lpp_id_fkey" FOREIGN KEY (lpp_id) REFERENCES lpps(id) ON = DELETE CASCADE DEFERRABLE > Referenced by: > TABLE "fileset_aix_file_maps" CONSTRAINT = "fileset_aix_file_maps_fileset_id_fkey" FOREIGN KEY (fileset_id) = REFERENCES filesets(id) ON DELETE CASCADE DEFERRABLE > TABLE "fileset_ptf_maps" CONSTRAINT = "fileset_ptf_maps_fileset_id_fkey" FOREIGN KEY (fileset_id) REFERENCES = filesets(id) ON DELETE CASCADE DEFERRABLE > TABLE "package_fileset_maps" CONSTRAINT = "package_fileset_maps_fileset_id_fkey" FOREIGN KEY (fileset_id) = REFERENCES filesets(id) ON DELETE CASCADE DEFERRABLE > TABLE "service_pack_fileset_maps" CONSTRAINT = "service_pack_fileset_maps_fileset_id_fkey" FOREIGN KEY (fileset_id) = REFERENCES filesets(id) ON DELETE CASCADE DEFERRABLE > TABLE "upd_pc_views" CONSTRAINT "upd_pc_views_fileset_id_fkey" = FOREIGN KEY (fileset_id) REFERENCES filesets(id) ON DELETE CASCADE = DEFERRABLE Thank you again for your help, pedz