Received: from localhost (unknown [200.46.204.184]) by developer.postgresql.org (Postfix) with ESMTP id 5CA102E00F1 for ; Wed, 23 Apr 2008 04:59:17 -0300 (ADT) Received: from developer.postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.184]) (amavisd-maia, port 10024) with ESMTP id 30344-01-7 for ; Wed, 23 Apr 2008 04:58:57 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.5 Received: from moutng.kundenserver.de (moutng.kundenserver.de [212.227.126.174]) by developer.postgresql.org (Postfix) with ESMTP id D99D02E0055 for ; Wed, 23 Apr 2008 04:58:12 -0300 (ADT) Received: from webserv.wug.de ([194.25.154.138]) by mrelayeu.kundenserver.de (node=mrelayeu5) with ESMTP (Nemesis) id 0ML25U-1JoZrm3gS9-0001if; Wed, 23 Apr 2008 09:58:11 +0200 Received: from kretschmer by webserv.wug.de with local (Exim 3.36 #1) id 1JoZrm-0003aW-00 for pgsql-performance@postgresql.org; Wed, 23 Apr 2008 09:58:10 +0200 Date: Wed, 23 Apr 2008 09:58:10 +0200 From: "A. Kretschmer" To: pgsql-performance@postgresql.org Subject: Re: mysterious difference in speed when combining two queries with OR Message-ID: <20080423075810.GB8401@a-kretschmer.de> References: <20080423072303.GF11886@amin> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20080423072303.GF11886@amin> X-OS: Debian/GNU Linux - weil ich es mir Wert bin! User-Agent: Mutt/1.5.9i X-Provags-ID: V01U2FsdGVkX18Hh7FHBKNzubth6y2B1wr5uI67x/JMMJkQZ2i asIXVCnfxA1qfpv4mm/bo6enxDd20G16QdDEdsIrz9gbmg+d9T /wlGH7KEC8AwBtNc5VlxDUbaxtnTmgq X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200804/343 X-Sequence-Number: 29836 am Wed, dem 23.04.2008, um 9:23:07 +0200 mailte Hans Ekbrand folgendes: > I cannot understand why the following two queries differ so much in execution time (almost ten times) wild guess: different execution plans. Can you show us the plans? (EXPLAIN ANALYSE SELECT ...) > > 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; > > [ snip ] > > I should say that this is on postgresql 7.4.16 (debian stable). Uhh. Why not a recent version? We have 8.3.0... > > Can query B be rewritten so that it would execute faster? Quick and dirty: use both selects (query A) combined with UNION. I guess, with a recent version the planner can use a bitmap index scan to perform Query B faster. Andreas -- Andreas Kretschmer Kontakt: Heynitz: 035242/47150, D1: 0160/7141639 (mehr: -> Header) GnuPG-ID: 0x3FFF606C, privat 0x7F4584DA http://wwwkeys.de.pgp.net