Received: from localhost (unknown [200.46.204.182]) by postgresql.org (Postfix) with ESMTP id 08A6D2E2FBA for ; Wed, 12 Dec 2007 19:33:00 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.182]) (amavisd-maia, port 10024) with ESMTP id 00302-03-7 for ; Wed, 12 Dec 2007 19:32:45 -0400 (AST) Received: from py-out-1112.google.com (py-out-1112.google.com [64.233.166.177]) by postgresql.org (Postfix) with ESMTP id 8FE052E0327 for ; Wed, 12 Dec 2007 19:30:33 -0400 (AST) Received: by py-out-1112.google.com with SMTP id u77so327894pyb.15 for ; Wed, 12 Dec 2007 15:30:32 -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=f1iHgOh6zm8OKA6bCSX7kBMTWIW2nIZHOK+OEDPmRHY=; b=k4yW8JL2Pcd9yC+TiWrp3WBfXIisaTCmte78Y9n5O37Z99GgpDZPAmW1oMn+PQ0N/B2lRYS2zKSYrGbp5FYTkTzqhyutTFcNYOUjb/Bqeh2itAK2hGdQ44NHogq5MUAGrNnNQX3kSLLwxbWamvuzCcVCgftkPqwWymSeAEUFxRk= 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=pD9NfOnC30wXSLAeiXHCVU0ZD1zJvOa1GC4XvA72unut1MBA2ShfDTddzUblxEiiLkxZYCg1i7IbmbUkySdPjdNCrk6X9mmkyi2X0bKcTAtduD+sYzpuefW1i9k/T2Zp/B801UYvhmqSMyHm0YQdnJEEnDXjwfwup8kff3IhqnY= Received: by 10.64.153.4 with SMTP id a4mr2765311qbe.47.1197502232061; Wed, 12 Dec 2007 15:30:32 -0800 (PST) Received: by 10.64.242.15 with HTTP; Wed, 12 Dec 2007 15:30:31 -0800 (PST) Message-ID: <8078a1730712121530r70a520e1qf9c09b516db54b0e@mail.gmail.com> Date: Wed, 12 Dec 2007 15:30:31 -0800 From: "Medi Montaseri" To: "Joshua D. Drake" Subject: Re: WHERE clause OR vs IN Cc: pgsql-admin@postgresql.org In-Reply-To: <20071212143632.3c742224@commandprompt.com> MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_Part_33352_4567054.1197502232045" References: <8078a1730712121425u51dca445j6f8f75468cdeda40@mail.gmail.com> <20071212143632.3c742224@commandprompt.com> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200712/73 X-Sequence-Number: 27590 ------=_Part_33352_4567054.1197502232045 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline 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 wrote: > -----BEGIN PGP SIGNED MESSAGE----- > Hash: SHA1 > > On Wed, 12 Dec 2007 14:25:16 -0800 > "Medi Montaseri" 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----- > ------=_Part_33352_4567054.1197502232045 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline 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-----

------=_Part_33352_4567054.1197502232045--