Re: PostgreSQL NOT IN performance

From: Віталій Тимчишин <tivv00(at)gmail(dot)com>
To: "Stephan Szabo" <sszabo(at)megazone(dot)bigpanda(dot)com>
Cc: pgsql-performance(at)postgresql(dot)org
Subject: Re: PostgreSQL NOT IN performance
Date: 2008-11-19 14:55:18
Message-ID: 331e40660811190655xccd1752rcb1ac7769e5cc8a4@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

2008/11/19 Stephan Szabo <sszabo(at)megazone(dot)bigpanda(dot)com>

>
> On Wed, 19 Nov 2008, [ISO-8859-5] Віталій Тимчишин wrote:
>
> > Query 1:
> > select * from t1 where id not in (select id from t2);
> >
> > Query 2 (gives same result as Q1):
> > select * from t1 except all (select id from t2);
>
> It gives the same result as long as no nulls are in either table. If
> either table can have a null, the conversion changes the results.
>
> In addition, a conversion like the above only happens to work because t1
> only has an id column. If t1 had two columns you'd get an error because
> the two sides of except all must have the same number of columns.
>

Actually It can be done even for multi-column mode if the selection is done
on unique key. It would look like:

select * from t1 inner join (
select id from t1 except select id from t2) talias on t1.id = talias.id

And it would produce better results then "not in" for large counts in t1 and
t2.

In response to

Browse pgsql-performance by date

  From Date Subject
Next Message Віталій Тимчишин 2008-11-19 15:12:43 Re: PostgreSQL NOT IN performance
Previous Message Stephan Szabo 2008-11-19 14:44:58 Re: PostgreSQL NOT IN performance