Re: [NOVICE] Function which gives back the

From: "Virgile Beddok" <virgile(dot)beddok(at)igd(dot)fraunhofer(dot)de>
To: "Tom Lane" <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: "Bruno Wolff III" <bruno(at)wolff(dot)to>, "Christopher Kings-Lynne" <chriskl(at)familyhealth(dot)com(dot)au>, "Virgile Beddok" <virgile(dot)beddok(at)igd(dot)fraunhofer(dot)de>, pgsql-novice(at)postgresql(dot)org, pgsql-advocacy(at)postgresql(dot)org
Subject: Re: [NOVICE] Function which gives back the
Date: 2005-03-28 15:52:21
Message-ID: 1218.146.140.8.94.1112025141.squirrel@webmail.igd.fraunhofer.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-admin pgsql-advocacy pgsql-novice

> Bruno Wolff III <bruno(at)wolff(dot)to> writes:
>> On Sun, Mar 27, 2005 at 13:24:34 +0800,
>> Christopher Kings-Lynne <chriskl(at)familyhealth(dot)com(dot)au> wrote:
>>>> I'm looking for an existing function which allows me to search the
>>>> nearest
>>>> neighbours of the requested value.
>>>
>>> Well you could try something like:
>>>
>>> SELECT * FROM table ORDER BY ABS(val - 2) LIMIT 1;
>>>
>>> That doesn't get you all the way there, but it's an idea...
>
>> For multidimensional objects you can do the same thing with a distance
>> metric function. It will be relatively slow since this won't be
>> indexable
>> and will require a sort of all of the values. If you have some bound on
>> how far apart points can be, then you might be able to limit the set
>> of candidate points using an indexable search.
>
> I'd probably go with looking for the nearest "above" neighbor and
> nearest "below" neighbor separately, eg
>
> select * from tab where val > 'target' order by val limit 1;
> select * from tab where val < 'target' order by val desc limit 1;
>
> If there's an index on val, this should work really well. Of course, if
> "nearest" is being defined in multidimensional terms as Bruno is
> imagining, it doesn't work at all...
>
> regards, tom lane

Thanks for the help.
I'll try this for the one-dimensional search.
For the muti-dimensional one, which tools of postgresql could I use for
this metric function, or this indexable search, which Bruno mentioned.
Do they already exist?
What about using a tree for that? Is there one which could fit to such a
"nearest neighbour search", or do I have to implement it myself...

In response to

Responses

Browse pgsql-admin by date

  From Date Subject
Next Message Bruno Wolff III 2005-03-28 15:57:15 Re: is postgresql 8 is realy mature
Previous Message Scott Marlowe 2005-03-28 15:23:14 Re: is postgresql 8 is realy mature

Browse pgsql-advocacy by date

  From Date Subject
Next Message Bruno Wolff III 2005-03-28 16:12:40 Re: [NOVICE] Function which gives back the nearest neighbours
Previous Message Mitch Pirtle 2005-03-27 21:23:47 Re: postgresQL.org "Latest News" or "Latest Product"?

Browse pgsql-novice by date

  From Date Subject
Next Message Bruno Wolff III 2005-03-28 16:12:40 Re: [NOVICE] Function which gives back the nearest neighbours
Previous Message John DeSoi 2005-03-28 13:39:28 Re: using psql