Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id D594A1337BB3 for ; Tue, 12 Oct 2010 21:23:35 -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 42635-06 for ; Wed, 13 Oct 2010 00:23:30 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from mxout-07.mxes.net (mxout-07.mxes.net [216.86.168.182]) by mail.postgresql.org (Postfix) with ESMTP id 1EE731336705 for ; Tue, 12 Oct 2010 21:23:27 -0300 (ADT) Received: from [10.0.1.5] (unknown [69.245.206.100]) by smtp.mxes.net (Postfix) with ESMTPA id 3478922E247; Tue, 12 Oct 2010 20:23:24 -0400 (EDT) Subject: Re: Query much faster with enable_seqscan=0 Mime-Version: 1.0 (Apple Message framework v1081) Content-Type: text/plain; charset=us-ascii From: Ogden In-Reply-To: <11723.1285111832@sss.pgh.pa.us> Date: Tue, 12 Oct 2010 19:23:24 -0500 Cc: pgsql-performance@postgresql.org Content-Transfer-Encoding: quoted-printable Message-Id: <9EE3D98A-CF01-43A3-B3AC-636F5D407FA8@darkstatic.com> References: <0D5B9ED0-00C3-4EEB-8336-0B2F8A884620@darkstatic.com> <11723.1285111832@sss.pgh.pa.us> To: Tom Lane X-Mailer: Apple Mail (2.1081) 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: 201010/188 X-Sequence-Number: 40607 On Sep 21, 2010, at 6:30 PM, Tom Lane wrote: > Ogden writes: >> SELECT tr.id, tr.sid >> FROM >> test_registration tr, >> INNER JOIN test_registration_result r on (tr.id =3D = r.test_registration_id) >> WHERE. >> = tr.test_administration_id=3D'32a22b12-aa21-11df-a606-96551e8f4e4c'::uuid >> GROUP BY tr.id, tr.sid >=20 > Seeing that tr.id is a primary key, I think you might be a lot better > off if you avoided the inner join and group by. I think what you = really > want here is something like >=20 > SELECT tr.id, tr.sid > FROM > test_registration tr > WHERE > = tr.test_administration_id=3D'32a22b12-aa21-11df-a606-96551e8f4e4c'::uuid > AND EXISTS(SELECT 1 FROM test_registration_result r > WHERE tr.id =3D r.test_registration_id) >=20 > regards, tom lane >=20 Thank you for this suggestion, however, what if I wanted some columns = from test_registration_result - this wouldn't work, for example if I = wanted test_registration_result.answer to be fetched. Hence, I had to = have a JOIN with test_registration_result and a GROUP BY. I still am not = happy with my query - the EXISTS executes in great speed however I = cannot retrieve any of the columns from that table.=20 Thank you Ogden