Re: Finding nearest numeric value

From: Ron Mayer <rm_pg(at)cheapcomplexdevices(dot)com>
To: Richard Huxton <dev(at)archonet(dot)com>
Subject: Re: Finding nearest numeric value
Date: 2005-08-17 18:57:52
Message-ID: 430388B0.1000709@cheapcomplexdevices.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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.

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Peter Fein 2005-08-17 19:00:06 Re: Finding nearest numeric value
Previous Message Ron Mayer 2005-08-17 18:45:55 Re: table clustering brings joy