Received: from localhost (maia-5.hub.org [200.46.204.182]) by postgresql.org (Postfix) with ESMTP id 04ECC9F94EC for ; Tue, 28 Aug 2007 16:29:13 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.182]) (amavisd-maia, port 10024) with ESMTP id 77151-02 for ; Tue, 28 Aug 2007 16:29:03 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.5 Received: from mail1.livedatagroup.com (mail1.livedatagroup.com [216.154.205.166]) by postgresql.org (Postfix) with ESMTP id BF5579F965A for ; Tue, 28 Aug 2007 16:29:05 -0300 (ADT) Received: from localhost (localhost [127.0.0.1]) by mail1.livedatagroup.com (Postfix) with ESMTP id 776F0B57FB; Tue, 28 Aug 2007 15:29:02 -0400 (EDT) Received: from mail1.livedatagroup.com ([192.168.3.224]) by localhost (mail1.livedatagroup.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 07170-05; Tue, 28 Aug 2007 15:28:59 -0400 (EDT) Received: from [192.168.2.134] (adsl-074-228-048-194.sip.bct.bellsouth.net [74.228.48.194]) by mail1.livedatagroup.com (Postfix) with ESMTP id 7B58CB576A; Tue, 28 Aug 2007 15:28:59 -0400 (EDT) Message-ID: <46D4777B.5030701@livedatagroup.com> Date: Tue, 28 Aug 2007 15:28:59 -0400 From: Pallav Kalva User-Agent: Thunderbird 1.5.0.10 (X11/20060911) MIME-Version: 1.0 To: Tom Lane CC: pgsql-performance@postgresql.org Subject: Re: 8.2.4 Chooses Bad Query Plan References: <46D43A8C.2080201@livedatagroup.com> <1206.1188326173@sss.pgh.pa.us> In-Reply-To: <1206.1188326173@sss.pgh.pa.us> Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Virus-Scanned: amavisd-new at livedatagroup.com X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200708/367 X-Sequence-Number: 26497 Hi Tom, Thanks! for the reply, see my comments below Tom Lane wrote: > Pallav Kalva writes: > >> We have recently upgraded our production database from 8.0.12 to >> 8.2.4, We have seen lot of improvements on 8.2.4 side but we are also >> seeing some queries which are slow. >> > > >> Particularly this below query is really bad in 8.2.4 , I can get >> only the explain on this as explain analyze never finishes even after 20 >> min. >> > > What it's doing is scanning backward on activity1_.activityid and hoping > to find a row that matches all the other constraints soon enough to make > that faster than any other way of doing the query. 8.0 would have done > the same thing, I believe, if the statistics looked favorable for it. > So I wonder if you've forgotten to re-ANALYZE your data since migrating > (a pg_dump script won't do this for you). > > So, if I understand this correctly it keeps doing index scan backwards until it finds a matching record , if it cant find any record it pretty much scans the whole table using "index scan backward" ? If I have no matching record I pretty much wait until the query finishes ? Is there anything else I can do to improve the query ? I have analyzed tables again and also my default_stats_target is set to 100, still it shows the same plan. >> -> Index Scan using idx_accountactivity_fkactivityid on >> accountactivity accountact0_ (cost=0.00..3.94 rows=1 width=16) >> Index Cond: (accountact0_.fkactivityid = >> activity1_.activityid) >> Filter: (fkaccountid = 1455437) >> > > >> -> Index Scan using >> idx_accountactivity_fkaccountid on accountactivity accountact0_ >> (cost=0.00..1641.42 rows=1343 width=16) (actual time=115.348..864.416 >> rows=10302 loops=1) >> Index Cond: (fkaccountid = 1455437) >> > > The discrepancy in rowcount estimates here is pretty damning. > Even the 8.0 estimate wasn't really very good --- you might want to > consider increasing default_statistics_target. > > regards, tom lane > > ---------------------------(end of broadcast)--------------------------- > TIP 6: explain analyze is your friend >