In the worst case, when you look at an unindexed field, using MIN() requires one full pass of the table. Using SORT and LIMIT requires file management. If you work with a large table, there will likely be a significant difference in performance. As a meaningless data point, MIN() took 0.36 s, and SORT and LIMIT took 0.84 from the 106,000 row table on my dev server.
If, however, you look at the indexed column, the difference becomes more difficult to notice (a pointless data point is 0.00 s in both cases). However, looking at the result of the explanation, it looks like MIN() can simply cut the smallest value from the index (“Select tables optimized” and “NULL”), while SORT and LIMIT still need to perform an ordered index traversal (106,000 rows) . The actual performance impact is likely to be negligible.
MIN() seems to be the way to go - it is faster in the worst case, indistinguishable in the best case, is standard SQL and most clearly expresses the value you are trying to get. The only case where it seems that using SORT and LIMIT would be desirable was indicated in mson , where you write a general operation that finds upper or lower N values from arbitrary columns and it is not worth writing a special case operation.
Sean McSomething Jan 09 '09 at 1:51 2009-01-09 01:51
source share