Re: NOT EXISTS or LEFT JOIN which one is better?

Lists: pgsql-performance
From: AI Rumman <rummandba(at)gmail(dot)com>
To: pgsql-performance(at)postgresql(dot)org
Subject: NOT EXISTS or LEFT JOIN which one is better?
Date: 2012-04-29 09:27:19
Message-ID: CAGoODpeU62n-gnE2NDMYwPGnszRv6=vmeJac8J=fuBMCNEZHBQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-performance

I can write a query to solve my requirement in any of the followings :-

1.
select *
from a
where NOT EXISTS
(
select 1
from b
where a.id = b.id)
union all
select *
from b

2.
select
(
case when b.id is not null then
b.id
else
a.id
) as id
from a
left join b
on a.id = b.id

Any one please tell me which one is better?


From: Rich <rhdyes(at)gmail(dot)com>
To: AI Rumman <rummandba(at)gmail(dot)com>
Cc: pgsql-performance(at)postgresql(dot)org
Subject: Re: NOT EXISTS or LEFT JOIN which one is better?
Date: 2012-04-29 15:10:22
Message-ID: CABDj5P0Zq=ipLyx9vNXWz1bzXx9D-Fok=XyXoKAkGHUEr83DpA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-performance

Al I have looked at this before and I am not sure the effort is worth all
the thought about it. Let your explain tell you which is better. I read
this link a year ago.
http://stackoverflow.com/questions/227037/can-i-get-better-performance-using-a-join-or-using-exists

On Sun, Apr 29, 2012 at 5:27 AM, AI Rumman <rummandba(at)gmail(dot)com> wrote:

> I can write a query to solve my requirement in any of the followings :-
>
> 1.
> select *
> from a
> where NOT EXISTS
> (
> select 1
> from b
> where a.id = b.id)
> union all
> select *
> from b
>
>
> 2.
> select
> (
> case when b.id is not null then
> b.id
> else
> a.id
> ) as id
> from a
> left join b
> on a.id = b.id
>
> Any one please tell me which one is better?
>