X-Original-To: pgsql-sql-postgresql.org@postgresql.org Received: from localhost (mx1.hub.org [200.46.208.251]) by postgresql.org (Postfix) with ESMTP id A47089FA7BA for ; Mon, 11 Sep 2006 01:55:12 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.208.251]) (amavisd-new, port 10024) with ESMTP id 02146-08 for ; Mon, 11 Sep 2006 01:55:03 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey- Received: from py-out-1112.google.com (py-out-1112.google.com [64.233.166.183]) by postgresql.org (Postfix) with ESMTP id AD6F19F9F80 for ; Mon, 11 Sep 2006 01:55:03 -0300 (ADT) Received: by py-out-1112.google.com with SMTP id d80so1994376pyd for ; Sun, 10 Sep 2006 21:55:03 -0700 (PDT) DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:message-id:date:from:sender:to:subject:cc:in-reply-to:mime-version:content-type:references:x-google-sender-auth; b=oPpejxgm6ayNw7Y1vlnG2/fiRmXfBJ30a2VkdheB4E53VsWnaMWciF0DzU/eLW4p10Cr3PlYCxiD2PqTeSqpGskq4/W7Nxr/yDx8oJfAW3dc813tQ+EUkcgSoUKJBFDMbky9wU7MiJCHPdxv/PH0iE6BakzvCIURwPMiT2PekV0= Received: by 10.35.88.17 with SMTP id q17mr8090830pyl; Sun, 10 Sep 2006 21:55:02 -0700 (PDT) Received: by 10.35.80.9 with HTTP; Sun, 10 Sep 2006 21:55:02 -0700 (PDT) Message-ID: Date: Sun, 10 Sep 2006 23:55:02 -0500 From: "Aaron Bono" To: "James Cloos" Subject: Re: where clause subqueries vs multiple rows results Cc: pgsql-sql@postgresql.org In-Reply-To: MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_Part_66373_3343338.1157950502260" References: X-Google-Sender-Auth: afc787bfe7ec879c X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.521 tagged_above=0 required=5 tests=AWL, DNS_FROM_RFC_ABUSE, HTML_50_60, HTML_MESSAGE, SPF_PASS X-Spam-Level: X-Archive-Number: 200609/61 X-Sequence-Number: 26272 ------=_Part_66373_3343338.1157950502260 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline Oops, see correction below... On 9/10/06, Aaron Bono wrote: > > On 9/10/06, James Cloos wrote: > > > I've a query which I'd have liked to word akin to: > > > > SELECT guid FROM child WHERE the_fkey = > > ( SELECT id FROM parent WHERE name ~ 'some_regex' ) > > ORDER BY the_fkey, my_pkey; > > > > I got around it by doing the SELECT id first, and then doing a SELECT > > guid for each row returned, appending the results together. > > > > Can that be done in a single query, insead of 1+n queries? > > > > Will this work? > > SELECT guid > FROM child > INNER JOIN parent ON ( > child.the_fkey = parent.id > ) > WHERE name ~ 'some_regex' > ORDER BY > child.the_fkey, > child.my_pkey > ; > > I am not sure what table name is from but since you say it is n+1 queries > I assume it is from the child table? It would help to know what columns are > on which tables. > > ================================================================== > Aaron Bono > Aranya Software Technologies, Inc. > http://www.aranya.com > http://codeelixir.com > ================================================================== > -- ================================================================== Aaron Bono Aranya Software Technologies, Inc. http://www.aranya.com http://codeelixir.com ================================================================== ------=_Part_66373_3343338.1157950502260 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline Oops, see correction below...

On 9/10/06, Aaron Bono <postgresql@aranya.com> wrote:
On 9/10/06, James Cloos <cloos@jhcloos.com> wrote:
I've a query which I'd have liked to word akin to:

  SELECT guid FROM child WHERE the_fkey =
         ( SELECT id FROM parent WHERE name ~ 'some_regex' )
         ORDER BY the_fkey, my_pkey;

I got around it by doing the SELECT id first, and then doing a SELECT
guid for each row returned, appending the results together.

Can that be done in a single query, insead of 1+n queries?

 
Will this work?

SELECT guid
FROM child
INNER JOIN parent ON (
    child.the_fkey = parent.id
)
WHERE name ~ 'some_regex'
ORDER BY
    child.the_fkey,
    child.my_pkey
;

I am not sure what table name is from but since you say it is n+1 queries I assume it is from the child table?  It would help to know what columns are on which tables.

==================================================================
   Aaron Bono
   Aranya Software Technologies, Inc.
   http://www.aranya.com
   http://codeelixir.com
==================================================================



--
==================================================================
   Aaron Bono
   Aranya Software Technologies, Inc.
   http://www.aranya.com
   http://codeelixir.com
================================================================== ------=_Part_66373_3343338.1157950502260--