Subject | Re: MAX vs. FIRST 1 |
---|---|
Author | Adam |
Post date | 2007-07-31T23:33:19Z |
> I always thought MAX is faster becausecome out
> I would have expected the database to simply consult the index and
> with the largest value, as opposed to first selecting all matchingrows,
> then sorting them and finally returning the first one. (Assuming Ihave an
> applicable index, obviously)It depends on the plan.
There are two ways to sort records when you have an index handy.
Either use the index to read them off disk in the order they need to
be (indexed walk), or to read them off disk in the order they are
stored, and sort them in memory (natural read then sort).
The former is much faster to get the first records, but in general
slower to complete. From my experience, Firebird tends to prefer the
indexed walk if you place a 'first n' condition into the query,
although I don't think I have ever needed to do it with a descending
index and query.
I would expect the 'max' and 'first 1 ... order by ... desc' to be
identical.
Adam