X-Original-To: pgsql-performance-postgresql.org@postgresql.org Received: from localhost (wm.hub.org [200.46.204.128]) by postgresql.org (Postfix) with ESMTP id 823589FB2CC for ; Fri, 15 Sep 2006 13:23:39 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.128]) (amavisd-new, port 10024) with ESMTP id 37788-05-4 for ; Fri, 15 Sep 2006 16:23:04 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey- Received: from olive.qinip.net (olive.qinip.net [62.100.30.40]) by postgresql.org (Postfix) with ESMTP id 91DD99FB2C1 for ; Fri, 15 Sep 2006 13:22:54 -0300 (ADT) Received: from [127.0.0.1] (h8441139206.dsl.speedlinq.nl [84.41.139.206]) by olive.qinip.net (Postfix) with ESMTP id A3E741807D; Fri, 15 Sep 2006 18:22:50 +0200 (MEST) Message-ID: <450AD35F.3070305@tweakers.net> Date: Fri, 15 Sep 2006 18:22:55 +0200 From: Arjen van der Meijden User-Agent: Thunderbird 1.5.0.5 (Windows/20060719) MIME-Version: 1.0 To: Tom Lane CC: Pallav Kalva , pgsql-performance@postgresql.org Subject: Re: Optimize SQL References: <450AC22F.9040004@livedatagroup.com> <2066.1158335599@sss.pgh.pa.us> In-Reply-To: <2066.1158335599@sss.pgh.pa.us> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Antivirus: avast! (VPS 0637-2, 15-09-2006), Outbound message X-Antivirus-Status: Clean X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0 tagged_above=0 required=5 tests= X-Spam-Level: X-Archive-Number: 200609/210 X-Sequence-Number: 20896 On 15-9-2006 17:53 Tom Lane wrote: > If that WHERE logic is actually what you need, then getting this query > to run quickly seems pretty hopeless. The database must form the full > outer join result: it cannot discard any listing0_ rows, even if they > have lastupdate outside the given range, because they might join to > addressval2_ rows within the given createdate range. And conversely > it can't discard any addressval2_ rows early. Is there any chance > that you wanted AND not OR there? Couldn't it also help to do something like this? SELECT ..., (SELECT MAX(createdate) FROM addressval ...) FROM listing l LEFT JOIN address ... WHERE l.id IN (SELECT id FROM listing WHERE lastupdate ... UNION SELECT id FROM listing JOIN addressval a ON ... WHERE a.createdate ...) Its not pretty, but looking at the explain only a small amount of records match both clauses. So this should allow the use of indexes for both the createdate-clause and the lastupdate-clause. Best regards, Arjen