Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id BB7E71337B83 for ; Wed, 13 Apr 2011 13:16:16 -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 02738-04 for ; Wed, 13 Apr 2011 16:15:37 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by mail.postgresql.org (Postfix) with ESMTP id BC34A1337BC4 for ; Wed, 13 Apr 2011 13:15:36 -0300 (ADT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.2/8.14.2) with ESMTP id p3DGFX0M006182; Wed, 13 Apr 2011 12:15:33 -0400 (EDT) To: Rob Sargent cc: pgsql-sql@postgresql.org Subject: Re: pass in array to function for use by where clause? how optimize? In-reply-to: <4DA5C5CB.3030907@gmail.com> References: <5079.1302707363@sss.pgh.pa.us> <4DA5C5CB.3030907@gmail.com> Comments: In-reply-to Rob Sargent message dated "Wed, 13 Apr 2011 09:48:27 -0600" Date: Wed, 13 Apr 2011 12:15:33 -0400 Message-ID: <6181.1302711333@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.91 tagged_above=-5 required=5 tests=BAYES_00=-1.9, T_RP_MATCHES_RCVD=-0.01 X-Spam-Level: X-Archive-Number: 201104/13 X-Sequence-Number: 35260 Rob Sargent writes: > On 04/13/2011 09:09 AM, Tom Lane wrote: >> Anish Kejariwal writes: >>> (select store_id, avg(sales) sales >>> from store >>> where group_id in(select $1[i] from generate_subscripts($1, 1) g(i)) >> Seems like a pretty brute-force way to deal with the array. Try >> where group_id = any($1) >> Not sure if it'll be a great deal faster, but it's at least easier to >> write. > Would adding a subservient function which actually uses the value of the > iteration (group_id) as a single parameter be of any use? Well, it'd be unlikely to solve the OP's actual complaint, which was performance. Hiding the semantics from the planner via a function (which generally looks like a black box to the planner) is even worse than hiding the semantics in a sub-SELECT, which I think is probably the root cause of the performance issue here. regards, tom lane