Received: from localhost (unknown [200.46.204.187]) by postgresql.org (Postfix) with ESMTP id B73912E32DE for ; Wed, 12 Dec 2007 22:27:18 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.187]) (amavisd-maia, port 10024) with ESMTP id 44236-06 for ; Wed, 12 Dec 2007 22:27:07 -0400 (AST) Received: from mu-out-0910.google.com (mu-out-0910.google.com [209.85.134.185]) by postgresql.org (Postfix) with ESMTP id 820F82E0245 for ; Wed, 12 Dec 2007 22:27:12 -0400 (AST) Received: by mu-out-0910.google.com with SMTP id i10so810720mue.3 for ; Wed, 12 Dec 2007 18:27:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:from:to:subject:cc:in-reply-to:mime-version:content-type:references; bh=H2EmXaRKwzTI1q3jjReGPfGboZO40ZooHJVSFJbhFtE=; b=ZtkhMDyGnOQS8PVSBRkhppMfNW6KHiGHOCiWaRxjz+iivYbukv9RGOtD45ffDWsWCfZG/+EALtLh25KUXPUHmUCYytu4bvbpInyNRlday+4KEccHQMpGkUhViAAc2IClWUfZMO5BbLQqPQfNjxcdtxeUVLheM+YuRvuQ3CbiSVU= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:to:subject:cc:in-reply-to:mime-version:content-type:references; b=OrvqMcaqoIv7EdESHXGDG0GCq1uD5ruES/bvyIJW+Th4aE4CREGuk3sS94m3762bMDXDx0scp55vx06VTJkiuaf9AcXYa/S1u6rEiZ+OIF9YW+8CZUv54DLfuo6unWIw23NVRPQueDiaw0Xo3/qpUxjS/L+wLA4LoR5KU/72ZjY= Received: by 10.64.3.9 with SMTP id 9mr3268197qbc.0.1197512830000; Wed, 12 Dec 2007 18:27:10 -0800 (PST) Received: by 10.64.242.15 with HTTP; Wed, 12 Dec 2007 18:27:09 -0800 (PST) Message-ID: <8078a1730712121827r76b20341o4b1cbb96eb05120c@mail.gmail.com> Date: Wed, 12 Dec 2007 18:27:09 -0800 From: "Medi Montaseri" To: "Tom Lane" Subject: Re: WHERE clause OR vs IN Cc: "Richard Broersma Jr" , pgsql-admin@postgresql.org In-Reply-To: <29422.1197507795@sss.pgh.pa.us> MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_Part_33797_23417817.1197512829970" References: <359993.87392.qm@web31808.mail.mud.yahoo.com> <29422.1197507795@sss.pgh.pa.us> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200712/78 X-Sequence-Number: 27595 ------=_Part_33797_23417817.1197512829970 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline 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 wrote: > Richard Broersma Jr writes: > > --- On Wed, 12/12/07, Medi Montaseri 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 > ------=_Part_33797_23417817.1197512829970 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline 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@sss.pgh.pa.us> wrote:
Richard Broersma Jr <rabroersma@yahoo.com> writes:
> --- On Wed, 12/12/07, Medi Montaseri <montaseri@gmail.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

------=_Part_33797_23417817.1197512829970--