Re: Query with Max, Order by is very slow.......

From: Bruno Wolff III <bruno(at)wolff(dot)to>
To: Hemapriya <priyam_1121(at)yahoo(dot)com>
Cc: pgsql-admin(at)postgresql(dot)org
Subject: Re: Query with Max, Order by is very slow.......
Date: 2004-04-08 01:57:32
Message-ID: 20040408015732.GA20746@wolff.to
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-admin

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

In response to

Browse pgsql-admin by date

  From Date Subject
Next Message Tom Lane 2004-04-08 02:07:01 Re: Query with Max, Order by is very slow.......
Previous Message Jaime Casanova 2004-04-07 23:43:15 Re: [admin] index in pk