Sebastian Janisch wrote:The first option (the inner select) will work slower and slower the more data is in the table, since the sql server does all the sorting. But in general sql servers are much faster processing data than if you did it in java.
The second one has a scaling issue too. It will return an increasingly higher percentage over useless data from the database to the application. In my experience, this performance hit is worse than the one on the query.
I would go with option #1. Also, option #1 can be tuned slightly - use a "?" instead of adding the maximum/minimum number to the
string. That way the prepared statement can be reused.