Re: Finding nearest numeric value - Mailing list pgsql-general

From Ron Mayer
Subject Re: Finding nearest numeric value
Date
Msg-id 430388B0.1000709@cheapcomplexdevices.com
Whole thread Raw
In response to Re: Finding nearest numeric value  (Richard Huxton <dev@archonet.com>)
Responses Re: Finding nearest numeric value  (Bruno Wolff III <bruno@wolff.to>)
List pgsql-general
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.

pgsql-general by date:

Previous
From: Ron Mayer
Date:
Subject: Re: table clustering brings joy
Next
From: "A. Kretschmer"
Date:
Subject: Re: postgres 8.x on debian