Re: WHERE clause OR vs IN

From: "Medi Montaseri" <montaseri(at)gmail(dot)com>
To: "Joshua D(dot) Drake" <jd(at)commandprompt(dot)com>
Cc: pgsql-admin(at)postgresql(dot)org
Subject: Re: WHERE clause OR vs IN
Date: 2007-12-12 23:30:31
Message-ID: 8078a1730712121530r70a520e1qf9c09b516db54b0e@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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(at)commandprompt(dot)com> wrote:

> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> On Wed, 12 Dec 2007 14:25:16 -0800
> "Medi Montaseri" <montaseri(at)gmail(dot)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-----
>

In response to

Responses

Browse pgsql-admin by date

  From Date Subject
Next Message Richard Broersma Jr 2007-12-13 00:37:45 Re: WHERE clause OR vs IN
Previous Message Joshua D. Drake 2007-12-12 22:36:32 Re: WHERE clause OR vs IN