问题
Question is simple. Which query will be faster:
SELECT TOP 1 value FROM table ORDER BY value
or
SELECT TOP 1 MIN(value) FROM table
We can assume that we have two cases, Case 1. No index and Case 2. With index on value.
Any insights are appreciated. Thanks!
回答1:
In the case where no index exists:
- MIN(value) should be implemented in O(N) time with a single scan;
- TOP 1 ... ORDER BY will require O(N Log N) time because of the specified sort (unless the DB engine is smart enough to read intent, which I would hate to rely on in production code).
When an index does exist:
- Both should require only O(1) time, using the index.
来源:https://stackoverflow.com/questions/15275968/performance-difference-select-top-1-order-by-vs-select-minval