pg.ddx.io  pgsql-performance@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Richard Huxton <dev@archonet.com>
To: David Teran <david.teran@cluster9.com>
Cc: PgSQL Performance ML <pgsql-performance@postgresql.org>
Subject: Re: select max(id) from aTable is very slow
Date: Thu, 03 Jun 2004 14:07:49 +0100
Message-ID: <40BF22A5.8060102@archonet.com> (raw)
In-Reply-To: <62A9E13D-60A0-11D8-B420-000A95A6F0DC@cluster9.com>
References: <62A9E13D-60A0-11D8-B420-000A95A6F0DC@cluster9.com>

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



view thread (5+ messages)

Message-ID: <40BF22A5.8060102@archonet.com>
Permalink:  ../40BF22A5.8060102@archonet.com/
Also on:    postgresql.org/message-id/40BF22A5.8060102@archonet.com

 · 

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 using the --to and --cc options:
  reply via email

  To: pgsql-performance@postgresql.org
  Cc: dev@archonet.com, david.teran@cluster9.com
  Subject: Re: select max(id) from aTable is very slow
  In-Reply-To: <40BF22A5.8060102@archonet.com>

* 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