Re: max(field) vs select field .. order by desc limit 1

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Lincoln Yeoh <lyeoh(at)pop(dot)jaring(dot)my>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: max(field) vs select field .. order by desc limit 1
Date: 2001-03-06 21:58:09
Message-ID: 7676.983915889@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Lincoln Yeoh <lyeoh(at)pop(dot)jaring(dot)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

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Neil Conway 2001-03-06 22:05:26 Re: DELETE and efficiency
Previous Message Tom Lane 2001-03-06 21:36:15 Re: PHP and PostgresSQL beta