pg.ddx.io pgsql-admin@postgresql.org mailing list archive
help / color / mirror / Atom feedWHERE clause OR vs IN
7+ messages / 4 participants
[nested] [flat]
* WHERE clause OR vs IN
@ 2007-12-12 22:25 Medi Montaseri <montaseri@gmail.com>
2007-12-12 22:36 ` Re: WHERE clause OR vs IN Joshua D. Drake <jd@commandprompt.com>
0 siblings, 1 reply; 7+ messages in thread
From: Medi Montaseri @ 2007-12-12 22:25 UTC (permalink / raw)
To: pgsql-admin
Hi,
I know this is not exactly admin related, but ... it is simple enough to be
even fun
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: WHERE clause OR vs IN
2007-12-12 22:25 WHERE clause OR vs IN Medi Montaseri <montaseri@gmail.com>
@ 2007-12-12 22:36 ` Joshua D. Drake <jd@commandprompt.com>
2007-12-12 23:30 ` Re: WHERE clause OR vs IN Medi Montaseri <montaseri@gmail.com>
0 siblings, 1 reply; 7+ messages in thread
From: Joshua D. Drake @ 2007-12-12 22:36 UTC (permalink / raw)
To: Medi Montaseri <montaseri@gmail.com>; +Cc: pgsql-admin
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
On Wed, 12 Dec 2007 14:25:16 -0800
"Medi Montaseri" <montaseri@gmail.com> wrote:
> Hi,
>
> I know this is not exactly admin related, but ... it is simple enough
> to be even fun
>
> From a performance point of view, is it better to use OR as in
> SELECT expr FROM expr WHERE col=this OR col=that OR col=theOther
> Or to use a range of values as in
> SELECT expr FROM expr WHERE col in ( val1, val2, ...., valn)
>
> I think the IN range yields a better query plan .... what do you
> think ? Thanks
Well a simple test:
postgres=# explain analyze select * from tellers where bid in ('1','2');
QUERY
PLAN
- ------------------------------------------------------------------------------------------------------
Seq Scan on tellers (cost=0.00..10.25 rows=20 width=352) (actual
time=0.019..0.192 rows=20 loops=1) Filter: (bid = ANY
('{1,2}'::integer[])) Total runtime: 0.314 ms (3 rows)
postgres=# explain analyze select * from tellers where bid = '1' or bid
= '2'; QUERY PLAN
- ------------------------------------------------------------------------------------------------------
Seq Scan on tellers (cost=0.00..11.50 rows=20 width=352) (actual
time=0.018..0.199 rows=20 loops=1) Filter: ((bid = 1) OR (bid = 2))
Total runtime: 0.275 ms
(3 rows)
postgres=#
> Medi
- --
The PostgreSQL Company: Since 1997, http://www.commandprompt.com/
Sales/Support: +1.503.667.4564 24x7/Emergency: +1.800.492.2240
Donate to the PostgreSQL Project: http://www.postgresql.org/about/donate
SELECT 'Training', 'Consulting' FROM vendor WHERE name = 'CMD'
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)
iD8DBQFHYGJwATb/zqfZUUQRAs32AJ9HuJVcY5gcr0hboxkI6PcRtv++JwCfXd00
nQ7Frkof0mVwqNYVxQ9Vziw=
=XzJi
-----END PGP SIGNATURE-----
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: WHERE clause OR vs IN
2007-12-12 22:25 WHERE clause OR vs IN Medi Montaseri <montaseri@gmail.com>
2007-12-12 22:36 ` Re: WHERE clause OR vs IN Joshua D. Drake <jd@commandprompt.com>
@ 2007-12-12 23:30 ` Medi Montaseri <montaseri@gmail.com>
2007-12-13 00:37 ` Re: WHERE clause OR vs IN Richard Broersma Jr <rabroersma@yahoo.com>
0 siblings, 1 reply; 7+ messages in thread
From: Medi Montaseri @ 2007-12-12 23:30 UTC (permalink / raw)
To: Joshua D. Drake <jd@commandprompt.com>; +Cc: pgsql-admin
Thanks...is'nt the run time latency more visiable with lorge input sets (big
tables) as well as how long the OR-ed expression chain is
based on your report, run time of OR is 0.275 ms and IN is 0.314
Perhaps if we run explain verbose to see the actual query plan
medi
On Dec 12, 2007 2:36 PM, Joshua D. Drake <jd@commandprompt.com> wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> On Wed, 12 Dec 2007 14:25:16 -0800
> "Medi Montaseri" <montaseri@gmail.com> wrote:
>
> > Hi,
> >
> > I know this is not exactly admin related, but ... it is simple enough
> > to be even fun
> >
> > From a performance point of view, is it better to use OR as in
> > SELECT expr FROM expr WHERE col=this OR col=that OR col=theOther
> > Or to use a range of values as in
> > SELECT expr FROM expr WHERE col in ( val1, val2, ...., valn)
> >
> > I think the IN range yields a better query plan .... what do you
> > think ? Thanks
>
> Well a simple test:
>
> postgres=# explain analyze select * from tellers where bid in ('1','2');
> QUERY
> PLAN
> -
> ------------------------------------------------------------------------------------------------------
> Seq Scan on tellers (cost=0.00..10.25 rows=20 width=352) (actual
> time=0.019..0.192 rows=20 loops=1) Filter: (bid = ANY
> ('{1,2}'::integer[])) Total runtime: 0.314 ms (3 rows)
>
> postgres=# explain analyze select * from tellers where bid = '1' or bid
> = '2'; QUERY PLAN
> -
> ------------------------------------------------------------------------------------------------------
> Seq Scan on tellers (cost=0.00..11.50 rows=20 width=352) (actual
> time=0.018..0.199 rows=20 loops=1) Filter: ((bid = 1) OR (bid = 2))
> Total runtime: 0.275 ms
> (3 rows)
>
> postgres=#
>
>
>
> > Medi
>
>
> - --
> The PostgreSQL Company: Since 1997, http://www.commandprompt.com/
> Sales/Support: +1.503.667.4564 24x7/Emergency: +1.800.492.2240
> Donate to the PostgreSQL Project: http://www.postgresql.org/about/donate
> SELECT 'Training', 'Consulting' FROM vendor WHERE name = 'CMD'
>
>
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.6 (GNU/Linux)
>
> iD8DBQFHYGJwATb/zqfZUUQRAs32AJ9HuJVcY5gcr0hboxkI6PcRtv++JwCfXd00
> nQ7Frkof0mVwqNYVxQ9Vziw=
> =XzJi
> -----END PGP SIGNATURE-----
>
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: WHERE clause OR vs IN
2007-12-12 22:25 WHERE clause OR vs IN Medi Montaseri <montaseri@gmail.com>
2007-12-12 22:36 ` Re: WHERE clause OR vs IN Joshua D. Drake <jd@commandprompt.com>
2007-12-12 23:30 ` Re: WHERE clause OR vs IN Medi Montaseri <montaseri@gmail.com>
@ 2007-12-13 00:37 ` Richard Broersma Jr <rabroersma@yahoo.com>
2007-12-13 01:03 ` Re: WHERE clause OR vs IN Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 1 reply; 7+ messages in thread
From: Richard Broersma Jr @ 2007-12-13 00:37 UTC (permalink / raw)
To: Medi Montaseri <montaseri@gmail.com>; +Cc: pgsql-admin
--- 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' );
and
SELECT T.*
FROM Tellers AS T
INNER JOIN ( VALUES ( '1' ), ( '2' ) ) AS B( bin )
ON T.bin = B.bin;
Regards,
Richard Broersma Jr.
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: WHERE clause OR vs IN
2007-12-12 22:25 WHERE clause OR vs IN Medi Montaseri <montaseri@gmail.com>
2007-12-12 22:36 ` Re: WHERE clause OR vs IN Joshua D. Drake <jd@commandprompt.com>
2007-12-12 23:30 ` Re: WHERE clause OR vs IN Medi Montaseri <montaseri@gmail.com>
2007-12-13 00:37 ` Re: WHERE clause OR vs IN Richard Broersma Jr <rabroersma@yahoo.com>
@ 2007-12-13 01:03 ` Tom Lane <tgl@sss.pgh.pa.us>
2007-12-13 01:17 ` Re: WHERE clause OR vs IN Richard Broersma Jr <rabroersma@yahoo.com>
2007-12-13 02:27 ` Re: WHERE clause OR vs IN Medi Montaseri <montaseri@gmail.com>
0 siblings, 2 replies; 7+ messages in thread
From: Tom Lane @ 2007-12-13 01:03 UTC (permalink / raw)
To: Richard Broersma Jr <rabroersma@yahoo.com>; +Cc: Medi Montaseri <montaseri@gmail.com>; pgsql-admin
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
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: WHERE clause OR vs IN
2007-12-12 22:25 WHERE clause OR vs IN Medi Montaseri <montaseri@gmail.com>
2007-12-12 22:36 ` Re: WHERE clause OR vs IN Joshua D. Drake <jd@commandprompt.com>
2007-12-12 23:30 ` Re: WHERE clause OR vs IN Medi Montaseri <montaseri@gmail.com>
2007-12-13 00:37 ` Re: WHERE clause OR vs IN Richard Broersma Jr <rabroersma@yahoo.com>
2007-12-13 01:03 ` Re: WHERE clause OR vs IN Tom Lane <tgl@sss.pgh.pa.us>
@ 2007-12-13 01:17 ` Richard Broersma Jr <rabroersma@yahoo.com>
1 sibling, 0 replies; 7+ messages in thread
From: Richard Broersma Jr @ 2007-12-13 01:17 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Medi Montaseri <montaseri@gmail.com>; pgsql-admin
--- On Wed, 12/12/07, Tom Lane <tgl@sss.pgh.pa.us> wrote:
> I seriously doubt that one's gonna win ...
:-)
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: WHERE clause OR vs IN
2007-12-12 22:25 WHERE clause OR vs IN Medi Montaseri <montaseri@gmail.com>
2007-12-12 22:36 ` Re: WHERE clause OR vs IN Joshua D. Drake <jd@commandprompt.com>
2007-12-12 23:30 ` Re: WHERE clause OR vs IN Medi Montaseri <montaseri@gmail.com>
2007-12-13 00:37 ` Re: WHERE clause OR vs IN Richard Broersma Jr <rabroersma@yahoo.com>
2007-12-13 01:03 ` Re: WHERE clause OR vs IN Tom Lane <tgl@sss.pgh.pa.us>
@ 2007-12-13 02:27 ` Medi Montaseri <montaseri@gmail.com>
1 sibling, 0 replies; 7+ messages in thread
From: Medi Montaseri @ 2007-12-13 02:27 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Richard Broersma Jr <rabroersma@yahoo.com>; 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@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
>
^ permalink raw reply [nested|flat] 7+ messages in thread
end of thread, other threads:[~2007-12-13 02:27 UTC | newest]
Thread overview: 7+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2007-12-12 22:25 WHERE clause OR vs IN Medi Montaseri <montaseri@gmail.com>
2007-12-12 22:36 ` Joshua D. Drake <jd@commandprompt.com>
2007-12-12 23:30 ` Medi Montaseri <montaseri@gmail.com>
2007-12-13 00:37 ` Richard Broersma Jr <rabroersma@yahoo.com>
2007-12-13 01:03 ` Tom Lane <tgl@sss.pgh.pa.us>
2007-12-13 01:17 ` Richard Broersma Jr <rabroersma@yahoo.com>
2007-12-13 02:27 ` Medi Montaseri <montaseri@gmail.com>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox