Re: max(field) vs select field .. order by desc limit 1 - Mailing list pgsql-general

From Tom Lane
Subject Re: max(field) vs select field .. order by desc limit 1
Date
Msg-id 7676.983915889@sss.pgh.pa.us
Whole thread Raw
In response to max(field) vs select field .. order by desc limit 1  (Lincoln Yeoh <lyeoh@pop.jaring.my>)
List pgsql-general
Lincoln Yeoh <lyeoh@pop.jaring.my> writes:
> Which would be better to do, max() or select .. order by limit 1?

If there's an index that the ORDER BY can key on, the second will be
much better.  If there's not, I'd go with the max(); you don't really
want a complete sort operation.

> Is 7.1 much better than 7.0.3 in doing the "limit 1"? I see that their
> EXPLAINs are different - 7.1 has a limit cost.

Should be about the same.  The appearance of a Limit plan node in 7.1
is an implementation change that was necessary to support LIMIT clauses
in subqueries, but it shouldn't make any noticeable performance
difference for top-level limits.

            regards, tom lane

pgsql-general by date:

Previous
From: Tom Lane
Date:
Subject: Re: PHP and PostgresSQL beta
Next
From: Neil Conway
Date:
Subject: Re: DELETE and efficiency