Re: Generic Q about max(id) vs ORDER BY ID DESC LIMIT 1 - Mailing list pgsql-general

From Michael Fuhr
Subject Re: Generic Q about max(id) vs ORDER BY ID DESC LIMIT 1
Date
Msg-id 20051024232930.GA26578@winnie.fuhr.org
Whole thread Raw
In response to Re: Generic Q about max(id) vs ORDER BY ID DESC LIMIT 1  (felix@crowfix.com)
Responses Re: Generic Q about max(id) vs ORDER BY ID DESC LIMIT 1  (Brendan Jurd <direvus@gmail.com>)
List pgsql-general
On Mon, Oct 24, 2005 at 03:50:57PM -0700, felix@crowfix.com wrote:
> I can't see any difference between these two statements:
>
>     SELECT MAX(id) FROM table;
>     SELECT id FROM table ORDER BY id DESC LIMIT 1;
>
> If the planner / optimizer / whatever doesn't optimize them to the
> same end result, is there a reason not to?  Is there a case for
> putting it on the TODO list?

Already done in 8.1.  Here's an excerpt from the Release Notes:

Automatically use indexes for MIN() and MAX() (Tom)

    In previous releases, the only way to use an index for MIN()
    or MAX() was to rewrite the query as SELECT col FROM tab ORDER
    BY col LIMIT 1.  Index usage now happens automatically.

--
Michael Fuhr

pgsql-general by date:

Previous
From: felix@crowfix.com
Date:
Subject: Re: Generic Q about max(id) vs ORDER BY ID DESC LIMIT 1 -- SOLVED
Next
From: Michael Fuhr
Date:
Subject: Re: Deleting vs foreign keys