Re: Slow performance on MAX(primary_key)

From: Ludwig Lim <lud_nowhere_man(at)yahoo(dot)com>
To: Keith Gray <keith(at)heart(dot)com(dot)au>
Cc: PostgreSQL Mailing List <pgsql-sql(at)postgresql(dot)org>
Subject: Re: Slow performance on MAX(primary_key)
Date: 2002-10-15 07:16:28
Message-ID: 20021015071628.57518.qmail@web80304.mail.yahoo.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql


Hi Keith:

--- Keith Gray <keith(at)heart(dot)com(dot)au> wrote:
> Help,
>
> I have just been comparing some large table
> performance
> under 7.1 using the
>
> select max(primary key)from table;
>
> We are using this for various functions including
> sequence.
>

Try using the following as alternative :

SELECT primary_key
FROM table
ORDER BY primary_key desc
LIMIT 1;

This should work if primary_key is indexes.

As of now, Max() doesn't utilizes the indices hence
it always do a sequential scan.

Hope that helps,

regards,
ludwig.

__________________________________________________
Do you Yahoo!?
New DSL Internet Access from SBC & Yahoo!
http://sbc.yahoo.com

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Keith Gray 2002-10-15 07:47:13 Re: Slow performance on MAX(primary_key)
Previous Message Brian Blaha 2002-10-15 06:06:03 SQL function triggers