X-Original-To: pgsql-performance-postgresql.org@postgresql.org Received: from localhost (mx1.hub.org [200.46.208.251]) by postgresql.org (Postfix) with ESMTP id 853209FB2A8 for ; Fri, 15 Sep 2006 12:53:41 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.208.251]) (amavisd-new, port 10024) with ESMTP id 86986-02 for ; Fri, 15 Sep 2006 12:53:30 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey- Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by postgresql.org (Postfix) with ESMTP id 9B3669FB277 for ; Fri, 15 Sep 2006 12:53:25 -0300 (ADT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.13.6/8.13.6) with ESMTP id k8FFrJxr002067; Fri, 15 Sep 2006 11:53:19 -0400 (EDT) To: Pallav Kalva cc: pgsql-performance@postgresql.org Subject: Re: Optimize SQL In-reply-to: <450AC22F.9040004@livedatagroup.com> References: <450AC22F.9040004@livedatagroup.com> Comments: In-reply-to Pallav Kalva message dated "Fri, 15 Sep 2006 11:09:35 -0400" Date: Fri, 15 Sep 2006 11:53:19 -0400 Message-ID: <2066.1158335599@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.106 tagged_above=0 required=5 tests=AWL, SPF_HELO_PASS, SPF_PASS X-Spam-Level: X-Archive-Number: 200609/207 X-Sequence-Number: 20893 Pallav Kalva writes: > select listing0_.listingid as col_0_0_, > getmaxdate(listing0_.lastupdate, max(addressval2_.createdate)) as col_1_0_ > from listing.listing listing0_ > left outer join listing.address listingadd1_ > on listing0_.fkbestaddressid=listingadd1_.addressid > left outer join listing.addressvaluation addressval2_ > on listingadd1_.addressid=addressval2_.fkaddressid > where listing0_.lastupdate>'2006-09-15 08:31:26.927' > and listing0_.lastupdate<=current_timestamp > or addressval2_.createdate>'2006-09-15 08:31:26.927' and > addressval2_.createdate<=current_timestamp > group by listing0_.listingid , listing0_.lastupdate > order by getmaxdate(listing0_.lastupdate, max(addressval2_.createdate)) > asc limit 10; 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? One thing that might help a bit is to change the join order: from listing.listing listing0_ left outer join listing.addressvaluation addressval2_ on listing0_.fkbestaddressid=addressval2_.fkaddressid left outer join listing.address listingadd1_ on listing0_.fkbestaddressid=listingadd1_.addressid so that at least the WHERE clause can be applied before having joined to listingadd1_. The semantics of your ON clauses are probably wrong anyway --- did you think twice about what happens if there's no matching listingadd1_ entry? regards, tom lane