Re: select max(id) from aTable is very slow - Mailing list pgsql-performance

From Richard Huxton
Subject Re: select max(id) from aTable is very slow
Date
Msg-id 40BF22A5.8060102@archonet.com
Whole thread Raw
In response to select max(id) from aTable is very slow  (David Teran <david.teran@cluster9.com>)
List pgsql-performance
David Teran wrote:
> Hi,
>
> we have a table with about 6.000.000 rows. There is an index on a
> column with the name id which is an integer and serves as primary key.
>
> When we execute select max(id) from theTable; it takes about 10
> seconds. Explain analyze returns:

Due to the open-ended nature of PG's aggregate function system, it can't
see inside the max() function to realise it doesn't need all the values.

Fortune favours the flexible however - the simple workaround is to use
the equivalent:
   SELECT id FROM theTable ORDER BY id DESC LIMIT 1;

--
   Richard Huxton
   Archonet Ltd

pgsql-performance by date:

Previous
From: Tom Lane
Date:
Subject: Re: PostgreSQL on VMWare vs Windows vs CoLinux
Next
From: Marcus Whitney
Date:
Subject: Re: Pl/Pgsql Functions running simultaneously