Re: nearest match

From: "Ryan Orth" <ryan(at)seahat(dot)com>
To: <jeff(at)propertykey(dot)com>
Cc: <pgsql-sql(at)postgresql(dot)org>
Subject: Re: nearest match
Date: 2003-03-13 17:22:21
Message-ID: 10193.65.102.128.233.1047576141.squirrel@fordparts.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

> Ryan wrote:
>> I'm doing some work with part diagrams and server-side image maps. I
>> want to store single point coordinates (x,y) for reference numbers in
>> a table looking like:
>>
>> reference_number text,
>> x int,
>> y int
>>
>> My question is: How can I find the *nearest* match of some clicked on
>> coordinates without specifying some arbitrary distance from the stored
>> point?
>
> How about something like this?
>
> select reference_number, '(50,50)'::point <-> point(x,y) as distance
> from my_table order by distance limit 1;
>
> With a reasonably small table, it should perform fairly well.

Hot damn! thats exactly what I needed. I imagine I would only be
comparing the distance of 50 points at any given time (about the max
number of reference numbers on any given image) so its mighty quick.
(184 points takes 1.80 msec to check)

I didn't even know about those geometric operators (I find new stuff every
day I swear). Are they SQL standard or postgres specific?

Ryan

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Kurt Overberg 2003-03-13 17:45:32 massive INSERT
Previous Message Tom Lane 2003-03-13 17:02:55 Re: [SQL] What's wrong with this group by clause?