pg.ddx.io  pgsql-admin@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Bruno Wolff III <bruno@wolff.to>
To: Hemapriya <priyam_1121@yahoo.com>
Cc: pgsql-admin@postgresql.org
Subject: Re: Query with Max, Order by is very slow.......
Date: Wed, 7 Apr 2004 20:57:32 -0500
Message-ID: <20040408015732.GA20746@wolff.to> (raw)
In-Reply-To: <20040407210354.79531.qmail@web21322.mail.yahoo.com>
References: <20040407210354.79531.qmail@web21322.mail.yahoo.com>

On Wed, Apr 07, 2004 at 14:03:54 -0700,
  Hemapriya <priyam_1121@yahoo.com> 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;



view thread (4+ messages)  latest in thread

Message-ID: <20040408015732.GA20746@wolff.to>
Permalink:  ../20040408015732.GA20746@wolff.to/
Also on:    postgresql.org/message-id/20040408015732.GA20746@wolff.to

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-admin@postgresql.org
  Cc: bruno@wolff.to, priyam_1121@yahoo.com
  Subject: Re: Query with Max, Order by is very slow.......
  In-Reply-To: <20040408015732.GA20746@wolff.to>
  References: <20040407210354.79531.qmail@web21322.mail.yahoo.com> <20040408015732.GA20746@wolff.to>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox