Asked By --CELKO--
31-Jul-08 11:58 PM

Please use real DDL instead of your personal shorthand. The data
element names make almost no sense. You have magical generic "id" and
a "parent_id" without any hint as to what standard they follow or how
the parent role relates to the other. what is the key?
UNIQUE(latitude, longitude)? Maybe you meant this?:
CREATE TABLE Locations
(location_id INTEGER NOT NULL PRIMARY KEY, -- industry standard used?
location_name CHAR(n) NOT NULL, -- not varchar? what is (n)?
latitude FLOAT NOT NULL,
longitude FLOAT NOT NULL,
parent_location_id INTEGER NOT NULL);
SELECT TOP 1 location_id, location_name, latitude, longitude,
dbo.Haversine(4.7131725, -74.057433, dbo.RevGeo.latitude,
dbo.RevGeo.longitude)AS distance
FROM RevGeo
ORDER BY distance;
The Haversine() function is killing you.
1) Can you buy a floating point processor for your computer? Probably
not, and I am not sure if SQL Server would use it.
2) Can you write the Haversine as in-line code and get rid of the row-
by-row function call? This will help a little.
I did a system like this years ago for a city-wide delivery system.
In the U.S. we have a series of map books called the Thomas Guides
(part of the Rand-McNally map company), which break the city into a
square grid defined by (page_nbr, x-coordinate, y-coordinate). You
can get an approximate distance with a simple Pythagorean distance
formula if the locations are on the same page. You use a look-up
table if they are not on the same page. You can buy the map
information from them on computer media.