pg.ddx.io  pgsql-admin@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Joshua D. Drake <jd@commandprompt.com>
To: Medi Montaseri <montaseri@gmail.com>
Cc: pgsql-admin@postgresql.org
Subject: Re: WHERE clause OR vs IN
Date: Wed, 12 Dec 2007 14:36:32 -0800
Message-ID: <20071212143632.3c742224@commandprompt.com> (raw)
In-Reply-To: <8078a1730712121425u51dca445j6f8f75468cdeda40@mail.gmail.com>
References: <8078a1730712121425u51dca445j6f8f75468cdeda40@mail.gmail.com>

-----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-----


view thread (7+ messages)  latest in thread

Message-ID: <20071212143632.3c742224@commandprompt.com>
Permalink:  ../20071212143632.3c742224@commandprompt.com/
Also on:    postgresql.org/message-id/20071212143632.3c742224@commandprompt.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-admin@postgresql.org
  Cc: jd@commandprompt.com, montaseri@gmail.com
  Subject: Re: WHERE clause OR vs IN
  In-Reply-To: <20071212143632.3c742224@commandprompt.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

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