Re: UNION versus SUB SELECT

From: Merlin Moncure <mmoncure(at)gmail(dot)com>
To: desmodemone <desmodemone(at)gmail(dot)com>
Cc: Robert DiFalco <robert(dot)difalco(at)gmail(dot)com>, "pgsql-performance(at)postgresql(dot)org" <pgsql-performance(at)postgresql(dot)org>
Subject: Re: UNION versus SUB SELECT
Date: 2013-11-22 15:54:29
Message-ID: CAHyXU0xGAO3rOoMQPmd4Otw98JHWOnZvrWbmQLVgWyzqiJGE1Q@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

On Thu, Nov 21, 2013 at 2:31 PM, desmodemone <desmodemone(at)gmail(dot)com> wrote:
> Hi Robert, could you try with "exists" ?
>
> SELECT c.*
> FROM contacts c
> WHERE exists ( SELECT 1 FROM phone p WHERE p.addr =? and
> p.contact_id=c.id )
> OR exists (SELECT 1 FROM email e WHERE e.addr = ? and e.contact_id=c.id );

hm, how about:
SELECT c.*
FROM contacts c
WHERE exists (
SELECT 1
FROM phone p
WHERE p.addr =? AND p.contact_id=c.id
UNION ALL
SELECT 1 FROM email e
WHERE e.addr = ? AND e.contact_id=c.id
);

merlin

In response to

Responses

Browse pgsql-performance by date

  From Date Subject
Next Message Robert DiFalco 2013-11-22 17:36:45 Re: UNION versus SUB SELECT
Previous Message Rogerio Pereira 2013-11-22 12:06:19 Re: Query in cache