Richard Huxton wrote:
While the other answers all do their job, and in one go too, I'd be
surprised if you found anything faster than:
SELECT myval FROM mytable WHERE myval > 1234 ORDER BY myval LIMIT 1
Really? Aren't most things with ORDER BY O(n*log(n))?
Or is the optimizer smart enough to find an index on myval
and stop after the first one (assuming the index returned
things sequentially.
If not, it seems this could do things in O(n) time:
select min(abs(value - CONSTANT)) from tablename
followed by
select * from tablename where abs(value - CONSTANT) = [result]
though I'm sure someone could roll that up into a single statement.
---------------------------(end of broadcast)---------------------------
TIP 3: Have you checked our extensive FAQ?
http://www.postgresql.org/docs/faq