X-Original-To: pgsql-admin-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.2]) by svr1.postgresql.org (Postfix) with ESMTP id CDD74D1D080 for ; Wed, 7 Apr 2004 22:54:01 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (neptune.hub.org [200.46.204.2]) (amavisd-new, port 10024) with ESMTP id 07223-03 for ; Wed, 7 Apr 2004 22:54:02 -0300 (ADT) Received: from wolff.to (wolff.to [66.93.249.74]) by svr1.postgresql.org (Postfix) with SMTP id 769FCD1D06C for ; Wed, 7 Apr 2004 22:53:58 -0300 (ADT) Received: (qmail 20811 invoked by uid 500); 8 Apr 2004 01:57:32 -0000 Date: Wed, 7 Apr 2004 20:57:32 -0500 From: Bruno Wolff III To: Hemapriya Cc: pgsql-admin@postgresql.org Subject: Re: Query with Max, Order by is very slow....... Message-ID: <20040408015732.GA20746@wolff.to> Mail-Followup-To: Hemapriya , pgsql-admin@postgresql.org References: <20040407210354.79531.qmail@web21322.mail.yahoo.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20040407210354.79531.qmail@web21322.mail.yahoo.com> User-Agent: Mutt/1.5.6i X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200404/133 X-Sequence-Number: 13106 On Wed, Apr 07, 2004 at 14:03:54 -0700, Hemapriya wrote: > Indexes: > "request_pkey" primary key, btree (origindb, uid) > > I do max Query like this > > select max(uid) from request where originDB=1; > > it took around 20 min to return the result.. Since > max, count functions do the full table scan, i tried > the workaround given.. > > select uid from request where originDB=1 order by uid > desc limit 1; > > this query runs forever.. i tried even without where > condition..no result.. Because the index is on both origindb and uid and the planner doesn't know that it can use this index when origindb is fixed but you are ordering on uid, you need to rewrite the query slightly. Try using: select uid from request where originDB=1 order by origindb desc, uid desc limit 1;