Skip site navigation (1) Skip section navigation (2)

Re: WHERE clause OR vs IN

From: "Medi Montaseri" <montaseri(at)gmail(dot)com>
To: "Tom Lane" <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: "Richard Broersma Jr" <rabroersma(at)yahoo(dot)com>, pgsql-admin(at)postgresql(dot)org
Subject: Re: WHERE clause OR vs IN
Date: 2007-12-13 02:27:09
Message-ID: 8078a1730712121827r76b20341o4b1cbb96eb05120c@mail.gmail.com (view raw or flat)
Thread:
Lists: pgsql-admin
Thanks everyone, and I know I am taking too much bandwidth, but...

The reason I was thinking IN would work better is that from a parser point
of view confronted with a series of expressions chained via boolean
operators such as
expr OR expr OR expr
The back end code generated has to be generic to combat with different type
of expression such as equality, greater than, less than, or any fancy
expressions. For example:
a == b OR c != d  AND e > f OR g == h
Where as in the case of
a IN (1, 2, 3, 4)
is actually a subset of the above composite expression because
1- the expressions are always equality of operand a with a literal value
which means the operand can be cached (register)
2- the composite expression is always an OR chained expression where the
first TRUE-ness would return the composite as TRUE (aka short circuit
behavior)

I could be wrong...I have been wrong before...

Medi


On Dec 12, 2007 5:03 PM, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:

> Richard Broersma Jr <rabroersma(at)yahoo(dot)com> writes:
> > --- On Wed, 12/12/07, Medi Montaseri <montaseri(at)gmail(dot)com> wrote:
> >> based on your report, run time of OR is 0.275 ms and IN is
> >> 0.314
> >>
> > postgres=# explain analyze select * from tellers where
> >> bid in ('1','2');
>
> > Two other options are:
>
> > SELECT *
> >   FROM Tellers
> >  WHERE bin = ANY( '1', '2' );
>
> Note that depending on which PG version you are testing, x IN (a,b,c)
> is exactly equivalent to x=a OR x=b OR x=c (older versions), or to
> x = ANY(ARRAYa,b,c]) (newer versions).
>
> >     SELECT T.*
> >       FROM Tellers AS T
> > INNER JOIN ( VALUES ( '1' ), ( '2' ) ) AS B( bin )
> >         ON T.bin = B.bin;
>
> I seriously doubt that one's gonna win ...
>
>                        regards, tom lane
>

In response to

pgsql-admin by date

Next:From: Rodrigo De LeónDate: 2007-12-13 03:25:50
Subject: Re: Schema security
Previous:From: Paul LambertDate: 2007-12-13 01:52:31
Subject: Schema security

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group