X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [64.117.224.130]) by svr1.postgresql.org (Postfix) with ESMTP id AD6D5D1B8A1 for ; Sun, 17 Aug 2003 15:38:16 +0000 (GMT) Received: from svr1.postgresql.org ([64.117.224.193]) by localhost (neptune.hub.org [64.117.224.130]) (amavisd-new, port 10024) with ESMTP id 93087-02 for ; Sun, 17 Aug 2003 12:38:06 -0300 (ADT) Received: from dyatel.antar.bryansk.ru (unknown [195.239.214.85]) by svr1.postgresql.org (Postfix) with ESMTP id BB9A7D1B523 for ; Sun, 17 Aug 2003 12:38:05 -0300 (ADT) Received: from fduch by dyatel.antar.bryansk.ru with local (Exim 4.21) id 19oPd4-0006Ac-9T; Sun, 17 Aug 2003 19:39:38 +0400 Date: Sun, 17 Aug 2003 19:39:38 +0400 From: "Alexander M. Pravking" To: Richard Huxton Cc: pgsql-sql@postgresql.org Subject: Re: Expression transformation curiosity Message-ID: <20030817153938.GA437@dyatel.antar.bryansk.ru> References: <200308171132.58073.dev@archonet.com> Mime-Version: 1.0 Content-Type: text/plain; charset=koi8-r Content-Disposition: inline In-Reply-To: <200308171132.58073.dev@archonet.com> User-Agent: Mutt/1.4.1i X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200308/206 X-Sequence-Number: 14822 On Sun, Aug 17, 2003 at 11:32:58AM +0100, Richard Huxton wrote: > I just had a look at EXPLAIN ANALYSE output for an SQL function I'm trying to > write. The WHERE clause wasn't too complex, but the explain output took up a > dozen lines. > > Boiling the problem down, I've looked at a clause of the form: > a OR (b AND c) > which PG converts to: > (a OR b) AND (a OR c) > > Now these two are equivalent, but it would take me forever to demonstrate that > with the full query. I'm happy the planner is going to get it right, but I'm > confused as to why the transformation occurs. > > Is it an artefact of displaying the EXPLAIN, or is it actually processed that > way? You could see how testing "a" twice could be expensive in some > situations. Looks like it actually works this way. I had the same problem several weeks ago on 7.3.3 with 4 such OR's. The final filter became monsterous, and the query was very slow. I've simply rewritten the query using UNION, and it became much faster. -- Fduch M. Pravking