Re: Distance calculation

From: Tino Wildenhain <tino(at)wildenhain(dot)de>
To: sunithab(at)travelpost(dot)com
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Distance calculation
Date: 2006-01-17 20:19:06
Message-ID: 43CD513A.8060503@wildenhain.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

sunithab(at)travelpost(dot)com schrieb:
> Hi
>
> I have a latiude and longitude for a city and latitude, longitude
> foreach hotel in hotels table.
>
> I have to reitreive 20 hotels nearby to that city in 25 miles. The
> below is the query I am using to check the distance. But the query is
> slow because of distance calulation on fly and order by distance.
>
> Can anybody help me how can I improve performance by refining lat and
> long data.

Actually I was using cube and earth datatype from contrib directory.

Instead of saving latiude/longitude I had earth (basically
a 0-dimensonal cube so you have 3-coordinates measured from
center of the earth in meters (you can also calculate in archaic
miles measurement by changing the constant for the earth radius -
see the files in contrib)

You can create an index on the earth-column (coordinates).

Next I was using cube_enlarge(earth_coordinates,radius,3) to get a
cube which covers nearest cities using the index:

cube_enlarge(start.coordinates,radius,3) @ cities.coordinates

now since you rules out a lot points far away you can fine scan
using earth_distance:

AND earth_distance(start.coordinates,cities.coordinates) < radius

this is really fast since only a couple of cities are outside
the circle (actually sphere) but inside the cube.

HTH
Tino

In response to

Browse pgsql-general by date

  From Date Subject
Next Message David Rysdam 2006-01-17 20:37:14 prepared statement results don't clear?
Previous Message Stephen Friedrich 2006-01-17 20:12:55 Re: Rule problem: return value of insert