X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 5639052827 for ; Thu, 19 May 2005 18:53:43 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 02525-04 for ; Thu, 19 May 2005 21:53:40 +0000 (GMT) Received: from hosting.commandprompt.com (128.commandprompt.com [207.173.200.128]) by svr1.postgresql.org (Postfix) with ESMTP id 9AFE85281E for ; Thu, 19 May 2005 18:53:39 -0300 (ADT) Received: from [192.168.1.52] (fc1smp [66.93.38.87]) (authenticated bits=0) by hosting.commandprompt.com (8.12.8/8.12.8) with ESMTP id j4JLjla3023179; Thu, 19 May 2005 14:45:47 -0700 Message-ID: <428D0AF0.4020001@commandprompt.com> Date: Thu, 19 May 2005 14:53:52 -0700 From: "Joshua D. Drake" Organization: Command Prompt, Inc. User-Agent: Mozilla Thunderbird 1.0.2-1.3.2 (X11/20050324) X-Accept-Language: en-us, en MIME-Version: 1.0 To: Jeffrey Tenny Cc: pgsql-performance@postgresql.org Subject: Re: Which is better, correlated subqueries or joins? References: <428D0A16.2010709@comcast.net> In-Reply-To: <428D0A16.2010709@comcast.net> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.016 tagged_above=0 required=5 tests=AWL X-Spam-Level: X-Archive-Number: 200505/327 X-Sequence-Number: 12491 Hello, It always depends on the dataset but you should try an explain analyze on each query. It will tell you which one is more efficient for your particular data. Sincerely, Joshua D. Drake > > Here's the join: > > # explain select child_pid from ssv_product_children, nv_products where > nv_products.id = ssv_product_children.child_pid and > ssv_product_children.pid = 1 and nv_products.isrel = 't'; > QUERY PLAN > -------------------------------------------------------------------------- > Hash Join (cost=1.22..2.47 rows=2 width=8) > Hash Cond: ("outer".child_pid = "inner".id) > -> Seq Scan on ssv_product_children (cost=0.00..1.18 rows=9 width=4) > Filter: (pid = 1) > -> Hash (cost=1.21..1.21 rows=4 width=4) > -> Seq Scan on nv_products (cost=0.00..1.21 rows=4 width=4) > Filter: (isrel = true) > (7 rows) > > > Here's the correlated subquery: > > > # explain select child_pid from ssv_product_children where pid = 1 and > child_pid = (select nv_products.id from nv_products where nv_products.id > = child_pid and isrel = 't'); > QUERY PLAN > --------------------------------------------------------------------- > Seq Scan on ssv_product_children (cost=0.00..18.78 rows=1 width=4) > Filter: ((pid = 1) AND (child_pid = (subplan))) > SubPlan > -> Seq Scan on nv_products (cost=0.00..1.26 rows=1 width=4) > Filter: ((id = $0) AND (isrel = true)) > (5 rows) > > > Thanks for any advice. > > ---------------------------(end of broadcast)--------------------------- > TIP 9: the planner will ignore your desire to choose an index scan if your > joining column's datatypes do not match -- Your PostgreSQL solutions company - Command Prompt, Inc. 1.800.492.2240 PostgreSQL Replication, Consulting, Custom Programming, 24x7 support Managed Services, Shared and Dedicated Hosting Co-Authors: plPHP, plPerlNG - http://www.commandprompt.com/