From: Hans Ekbrand <hans.ekbrand@sociology.gu.se>
To: pgsql-performance@postgresql.org
Subject: mysterious difference in speed when combining two queries with OR
Date: Wed, 23 Apr 2008 09:23:07 +0200
Message-ID: <20080423072303.GF11886@amin> (raw)
I cannot understand why the following two queries differ so much in execution time (almost ten times)
Query A (two queries)
select distinct moment.mid from moment,timecard where parent = 45 and (pid=17 and timecard.mid = moment.mid) order by moment.mid;
select distinct moment.mid from moment,timecard where parent = 45 and (pbar = 0) order by moment.mid;
Query B (combining the two with OR)
select distinct moment.mid from moment,timecard where parent = 45 and ((pid=17 and timecard.mid = moment.mid) or (pbar = 0)) order by moment.mid;
$ time psql -o /dev/null -f query-a.sql fektest
real 0m2.016s
user 0m1.532s
sys 0m0.140s
$ time psql -o /dev/null -f query-b.sql fektest
real 0m28.534s
user 0m1.516s
sys 0m0.156s
I have tested this in two different computers with different amount of
RAM, fast or slow CPU, and the difference is persistent, almost ten
times.
I should say that this is on postgresql 7.4.16 (debian stable).
Can query B be rewritten so that it would execute faster?
TIA
--
Hans Ekbrand (http://sociologi.cjb.net) <hans@sociologi.cjb.net>
GPG Fingerprint: 1408 C8D5 1E7D 4C9C C27E 014F 7C2C 872A 7050 614E
Attachments:
signature.asc (application/pgp-signature, 188B) download
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-performance@postgresql.org
Cc: hans.ekbrand@sociology.gu.se
Subject: Re: mysterious difference in speed when combining two queries with OR
In-Reply-To: <20080423072303.GF11886@amin>
* 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