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 51E559FA441 for ; Thu, 6 Jul 2006 22:51:15 -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 25445-03 for ; Thu, 6 Jul 2006 22:51:06 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey- Received: from nz-out-0102.google.com (nz-out-0102.google.com [64.233.162.198]) by postgresql.org (Postfix) with ESMTP id 7DE1A9F94F3 for ; Thu, 6 Jul 2006 22:51:06 -0300 (ADT) Received: by nz-out-0102.google.com with SMTP id 8so1411986nzo for ; Thu, 06 Jul 2006 18:51:04 -0700 (PDT) DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:message-id:date:from:user-agent:mime-version:to:cc:subject:references:in-reply-to:content-type:content-transfer-encoding; b=LPp1RmH9/hoE9OPhHPdEbd+9+91UlQaHOah7cIMYQwgIBvjemKKhqG4pon+w1ulpz3FXi/7KhDx7qTdqo4sEvBMAoaENeZHA5zTvCb4P6MTr9THkhe+FIEXlLzmnOiM3IVVLtLnpHnQ+MmhVERiqv96HxRMr2G3KmGGuTapyTAY= Received: by 10.36.118.8 with SMTP id q8mr1807850nzc; Thu, 06 Jul 2006 18:51:04 -0700 (PDT) Received: from ?10.1.1.20? ( [203.217.18.65]) by mx.gmail.com with ESMTP id 14sm3819638nzp.2006.07.06.18.51.02; Thu, 06 Jul 2006 18:51:03 -0700 (PDT) Message-ID: <44ADBE04.9010605@gmail.com> Date: Fri, 07 Jul 2006 11:51:00 +1000 From: Chris User-Agent: Thunderbird 1.5.0.4 (Windows/20060516) MIME-Version: 1.0 To: Ioana Danes CC: pgsql-performance@postgresql.org Subject: Re: Query plan issue when upgrading to postgres 8.14 (from References: <20060706200652.50294.qmail@web55911.mail.re3.yahoo.com> In-Reply-To: <20060706200652.50294.qmail@web55911.mail.re3.yahoo.com> Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 8bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200607/56 X-Sequence-Number: 19916 Ioana Danes wrote: > I have a problem with a query that in postgres 7.4 and 8.12 has an > acceptable response time but in postgres 8.14 is very slow. > > This is the table I use: > * > create* *table* TEST ( > TESTID INT8 *not* *null*, > TESTTYPE INT4 *null*, > *constraint* PK_TESTID *primary* *key* (TESTID)); > *create* *index* IX_TEST_TESTTYPE *on* TEST (TESTTYPE); > > And this is the query with the problem: > > *explain select* *max*(TESTID) *from* TEST *where* TESTTYPE = 1577; > > The query plan in postgres 7.4 and 8.12 is using the index by TESTTYPE > field, which is what I want in this case. > > QUERY PLAN > Aggregate (cost=25.97..25.97 rows=1 width=8) > -> Index Scan using ix_test_testtype on test (cost=0.00..25.95 > rows=9 width=8) > Index Cond: (testtype = 1577) > > > With postgres 8.14 the query plan uses the primary key PK_TESTID with > filter by TESTTYPE, which it takes almost 10 minutes to execute: > > QUERY PLAN > Limit (cost=0.00..41.46 rows=1 width=8) > -> Index Scan Backward using pk_testid on test (cost=…) > Filter: ((testid IS NOT NULL) and (testtype = 1577)) > > When replacing the index > *create* *index* IX_TEST_TESTTYPE *on* TEST (TESTTYPE); > with > *create* *index* IX_TEST_TESTTYPE *on* TEST (TESTTYPE, TESTID); > the query plan uses this index and the execution of this select is > extremely fast. > > From what I can see, the query plan for 8.14 is using a index scan by > the field used with max() function with a filter by the field in where > condition. > Should not the query plan use an index scan by the field in where > condition (which in my case is a small range) and come up with the max > value in that range? > > Is this a bug, am I missing a configuration step or this is how it is > supposed to work? You've left out the best details. Post an 'explain analyze' from both versions, and don't cut anything out :) I'm guessing postgres is seeing an index on the table is faster because it doesn't think you have many rows in the table. How many are there, and have you done an analyze of the table after loading the data in? -- Postgresql & php tutorials http://www.designmagick.com/