Re: haversine formula with postgreSQL

From: Merlin Moncure <mmoncure(at)gmail(dot)com>
To: Scott Bailey <artacus(at)comcast(dot)net>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: haversine formula with postgreSQL
Date: 2009-09-19 15:00:56
Message-ID: b42b73150909190800j753c7349jb4f31b8ceab4184a@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Fri, Sep 18, 2009 at 5:50 PM, Scott Bailey <artacus(at)comcast(dot)net> wrote:
>> And I think that might work for you.  The performance is going to be
>> miserable for large stat sets, because it's going to scan the whole
>> aaafacilities table every time and recompute every distance, but as an
>> example goes it's probably acceptable.
>
> Something I did when implementing haversine in Oracle was first calculate a
> bounding box for lat/lon and only calculate the distance for cities w/in the
> bounding box.

This is easily done in postgresql with gist operator on box type. In
some cases you can actually optimize it even further...you can make a
'inner' bounding box that captures points that do not need the
expensive distance function. So you use gist to pull points that
'may' be inside the distance, box overlap the points that 'must' be
inside the distance, and distance function the rest.

Also I highly suggest moving the distance calculation inside an
immutable function.

merlin

In response to

Browse pgsql-general by date

  From Date Subject
Next Message David Fetter 2009-09-19 15:05:59 Re: Return 30% of results?
Previous Message 纪晓曦 2009-09-19 13:06:58 Re: Does PG cache results of an aggregate function, (and results of non-volatile functions)?