Received: from localhost (maia-1.hub.org [200.46.204.191]) by postgresql.org (Postfix) with ESMTP id EA51A9FB63A for ; Mon, 9 Apr 2007 17:37:38 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.191]) (amavisd-maia, port 10024) with ESMTP id 37172-01-2 for ; Mon, 9 Apr 2007 17:37:34 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.4 Received: from an-out-0708.google.com (an-out-0708.google.com [209.85.132.251]) by postgresql.org (Postfix) with ESMTP id 5DB689FB5F0 for ; Mon, 9 Apr 2007 17:37:34 -0300 (ADT) Received: by an-out-0708.google.com with SMTP id c24so1766522ana for ; Mon, 09 Apr 2007 13:37:33 -0700 (PDT) DKIM-Signature: a=rsa-sha1; c=relaxed/relaxed; d=gmail.com; s=beta; h=domainkey-signature:received:received:message-id:date:from:to:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=D/WXLzN5Tgt9gcJ84gk7cxdhRKIn1fXo5z2z3naTmn2mlpN97QLFHahFu1dhxnzCImXzfzi6+UM8dRyRXtI6VeJL5bnrS+nsHPqBDSMZB4Dev913/Euwz8QUEhMj7u8PWWTa8i/SXLUbMY/txAsoEU/7fK3ZJcjhmctiq5ibhI4= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=beta; h=received:message-id:date:from:to:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=cyaZOYPgsxw+wVqFNY5q+Oe7HNRu7SjSYQnv8nAeuTjZfNdjgaSDHc9FmDijtNs/EhcyBKU2uHv1POCCxzKhTJEABiL2Zj2eRKxWamhbnIeqM0133+y5WAN533muz5Z6BxmGXJOuonumklR1bdt9OS0jb3tnoDKBKh5IZRGq7N0= Received: by 10.100.44.13 with SMTP id r13mr4357296anr.1176151053484; Mon, 09 Apr 2007 13:37:33 -0700 (PDT) Received: by 10.100.247.7 with HTTP; Mon, 9 Apr 2007 13:37:33 -0700 (PDT) Message-ID: Date: Mon, 9 Apr 2007 16:37:33 -0400 From: "Alex Deucher" To: "Michael Fuhr" Subject: Re: postgres 8.2 seems to prefer Seq Scan Cc: "PostgreSQL Performance" In-Reply-To: MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline References: <20070406213147.GA12788@winnie.fuhr.org> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200704/210 X-Sequence-Number: 24087 On 4/9/07, Alex Deucher wrote: > On 4/9/07, Alex Deucher wrote: > > On 4/6/07, Michael Fuhr wrote: > > > On Fri, Apr 06, 2007 at 04:38:33PM -0400, Alex Deucher wrote: > > > > One more anomaly between 7.4 and 8.2. DB dumped from 7.4 and loaded > > > > onto 8.2, both have locale set to C. 8.2 seems to prefer Seq Scans > > > > for the first query while the ordering in the second query seems to > > > > perform worse on 8.2. I ran analyze. I've tried with the encoding > > > > set to UTF-8 and SQL_ASCII; same numbers and plans. Any ideas how to > > > > improve this? > > > > > > Are you sure the data sets are identical? The 7.4 query returned > > > 0 rows; the 8.2 query returned 1 row. If you're running the same > > > query against the same data in both versions then at least one of > > > them appears to be returning the wrong result. Exactly which > > > versions of 7.4 and 8.2 are you running? > > > > > > Have you analyzed all tables in both versions? The row count > > > estimate in 7.4 is much closer to reality than in 8.2: > > > > > > 7.4 > > > > -> Index Scan using pnum_idx on event (cost=0.00..3.37 rows=19 > > > > width=172) (actual time=0.063..0.063 rows=0 loops=1) > > > > Index Cond: ((pnum)::text = 'AB5819188'::text) > > > > > > 8.2 > > > > -> Index Scan using pnum_idx on event (cost=0.00..3147.63 > > > > rows=1779 width=171) (actual time=0.030..0.033 rows=1 loops=1) > > > > Index Cond: ((pnum)::text = 'AB5819188'::text) > > > > > > If analyzing the event table doesn't improve the row count estimate > > > then try increasing the statistics target for event.pnum and analyzing > > > again. Example: > > > > > > ALTER TABLE event ALTER pnum SET STATISTICS 100; > > > ANALYZE event; > > > > > > You can set the statistics target as high as 1000 to get more > > > accurate results at the cost of longer ANALYZE times. > > > > > > > Setting statistics to 400 seems to be the sweet spot. Values above > > that seem to only marginally improve performance. However, I have to > > disable seqscan in order for the query to be fast. Why does the query > > planner insist on doing a seq scan? Is there anyway to make it prefer > > the index scan? > > Ok, it looks like bumping up the the stats to 400 did the trick. It seems my test sets were not a good representation of the queries. The sets I was using were more of an exception to the rule since they were hitting comparatively fewer rows that most others. Thanks to everyone on the list and IRC for their help. Alex