Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1U7hfW-0006WX-Tm for pgsql-performance@arkaria.postgresql.org; Tue, 19 Feb 2013 07:31:15 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.72) (envelope-from ) id 1U7hfW-0000ng-4H for pgsql-performance@arkaria.postgresql.org; Tue, 19 Feb 2013 07:31:14 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1U7hfU-0000nS-1Q for pgsql-performance@postgresql.org; Tue, 19 Feb 2013 07:31:12 +0000 Received: from mailout12.yourhostingaccount.com ([65.254.253.101]) by magus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1U7hfP-0006lM-UV for pgsql-performance@postgresql.org; Tue, 19 Feb 2013 07:31:11 +0000 Received: from mailscan07.yourhostingaccount.com ([10.1.15.7] helo=mailscan07.yourhostingaccount.com) by mailout12.yourhostingaccount.com with esmtp (Exim) id 1U7hfO-0004gm-0e for pgsql-performance@postgresql.org; Tue, 19 Feb 2013 02:31:06 -0500 Received: from impout02.yourhostingaccount.com ([10.1.55.2] helo=impout02.yourhostingaccount.com) by mailscan07.yourhostingaccount.com with esmtp (Exim) id 1U7hfN-0002hE-Kx for pgsql-performance@postgresql.org; Tue, 19 Feb 2013 02:31:05 -0500 Received: from authsmtp17.yourhostingaccount.com ([10.1.18.17]) by impout02.yourhostingaccount.com with NO UCE id 27X51l0060N5tVm017X5ru; Tue, 19 Feb 2013 02:31:05 -0500 X-Authority-Analysis: v=2.0 cv=HIVB5/Rv c=1 sm=1 a=EIOe3WKORclhkPTkdY9Vuw==:17 a=kUA5AuSWobEA:10 a=jl2ymW9NZSsA:10 a=eybWMMEvRm0A:10 a=ErrjNDgeMrkA:10 a=8nJEP1OIZ-IA:10 a=cuFqp0DKAAAA:8 a=6uajheNnxxwA:10 a=VmV5dO-aDA543sYhYnsA:9 a=wPNLvfGTeEIA:10 a=zNgDkQsFS4IA:10 a=qes8CxgSzUMA:10 a=ayZJSlMgWVhgG3n+ZwULew==:117 X-EN-OrigOutIP: 10.1.18.17 X-EN-IMPSID: 27X51l0060N5tVm017X5ru Received: from [94.186.167.42] (helo=[127.0.0.1]) by authsmtp17.yourhostingaccount.com with esmtpa (Exim) id 1U7hfN-0004Ci-14 for pgsql-performance@postgresql.org; Tue, 19 Feb 2013 02:31:05 -0500 Message-ID: <51232A36.6000701@orprovision.com> Date: Tue, 19 Feb 2013 08:31:02 +0100 From: Andy Reply-To: andy.gumbrecht@orprovision.com Organization: OrproVision.com User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/20130107 Thunderbird/17.0.2 MIME-Version: 1.0 To: pgsql-performance@postgresql.org Subject: Re: Speed of exist References: <51231D10.1070101@basenlily.me> In-Reply-To: <51231D10.1070101@basenlily.me> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 8bit X-EN-UserInfo: 50dabef77e03926a71da0ab8beb9457f:3688b5c6977cb6d9a4107f0892e52205 X-EN-AuthUser: andy.gumbrecht@orprovision.com X-EN-OrigIP: 94.186.167.42 X-EN-OrigHost: unknown X-Pg-Spam-Score: -2.5 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-performance Precedence: bulk Sender: pgsql-performance-owner@postgresql.org Limit the sub-queries to 1, i.e. : select 1 from Table2 where Table2.ForeignKey = Table1.PrimaryKey fetch first 1 rows only Andy. On 19.02.2013 07:34, Bastiaan Olij wrote: > Hi All, > > Hope someone can help me a little bit here: > > I've got a query like the following: > -- > select Column1, Column2, Column3 > from Table1 > where exists (select 1 from Table2 where Table2.ForeignKey = > Table1.PrimaryKey) > or exists (select 1 from Table3 where Table3.ForeignKey = Table1.PrimaryKey) > -- > > Looking at the query plan it is doing a sequential scan on both Table2 > and Table3. > > If I remove one of the subqueries and turn the query into: > -- > select Column1, Column2, Column3 > from Table1 > where exists (select 1 from Table2 where Table2.ForeignKey = > Table1.PrimaryKey) > -- > > It is nicely doing an index scan on the index that is on Table2.ForeignKey. > > As Table2 and Table3 are rather large the first query takes minutes > while the second query takes 18ms. > > Is there a way to speed this up or an alternative way of selecting > records from Table1 which have related records in Table2 or Table3 which > is faster? > > Kindest Regards, > > Bastiaan Olij > > > -- ------------------------------------------------------------------------------------------------------------------------ *Andy Gumbrecht* Research & Development Orpro Vision GmbH Hefehof 24, 31785, Hameln +49 (0) 5151 809 44 21 +49 (0) 1704 305 671 andy.gumbrecht@orprovision.com www.orprovision.com Orpro Vision GmbH Sitz der Gesellschaft: 31785, Hameln USt-Id-Nr: DE264453214 Amtsgericht Hannover HRB204336 Geschaeftsfuehrer: Roberto Gatti, Massimo Gatti, Adam Shaw ------------------------------------------------------------------------------------------------------------------------ Diese E-Mail enthält vertrauliche und/oder rechtlich geschützte Informationen. Wenn Sie nicht der richtige Adressat sind oder diese E-Mail irrtümlich erhalten haben, informieren Sie bitte sofort den Absender und vernichten Sie diese Mail. Das unerlaubte Kopieren, jegliche anderweitige Verwendung sowie die unbefugte Weitergabe dieser Mail ist nicht gestattet. ------------------------------------------------------------------------------------------------------------------------ This e-mail may contain confidential and/or privileged information. If you are not the intended recipient (or have received this e-mail in error) please notify the sender immediately and destroy this e-mail. Any unauthorized copying, disclosure, distribution or other use of the material or parts thereof is strictly forbidden. ------------------------------------------------------------------------------------------------------------------------ -- Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-performance