473,503 Members | 10,245 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

zip distance in a square, not radius

I have successfully created a zip code radius search, but the performance is
unacceptable.

I have two tables. The first is 52K zip codes w/lat and long. The second is
3K national business addresses.

Using the zip code 10010, it takes almost 60 seconds to return 122
businesses within a 25 mile radius. This slowdown is almost entirely due to
the query radius distance formula.

I wonder if someone knows the formula to calculate the distance within a
given square. Would this increase performance? I presume the results would
include zips in the "corners", but performance boost might be worth it.

Thanks in advance.
Jul 17 '05 #1
4 3803
The trick is to use the box to quickly filter out the ones that can't
possibly satistfy the condition, before doing the more expensive radius
calculation.

First of all, add the latitude and longtitude of each business to the second
table. Since these are going to stay the same through difference searches,
we can do the look up in advance. Be sure to put indexes on the columns.

Using the lat., long. of the zip code provide by the user, calculate the box

$left = $long - $dist; $right = $long + $dist;
$top = $lat + $dist; $bottom = $lat - $dist;

and use it in the query to exclude those businesses outside the box.

$result = mysql_query("SELECT * FROM business
WHERE ($left < long) AND (long < $right)
AND ($bottom < lat) AND (lat < $top);

Now, if you want, you can do a radius check one each row. It's probably
unnecessary though, since you don't actually know precise location of the
user and the businesses.
Uzytkownik "Xenophobe" <xe*******@planetx.com> napisal w wiadomosci
news:sSAZb.91266$jk2.439590@attbi_s53...
I have successfully created a zip code radius search, but the performance is unacceptable.

I have two tables. The first is 52K zip codes w/lat and long. The second is 3K national business addresses.

Using the zip code 10010, it takes almost 60 seconds to return 122
businesses within a 25 mile radius. This slowdown is almost entirely due to the query radius distance formula.

I wonder if someone knows the formula to calculate the distance within a
given square. Would this increase performance? I presume the results would
include zips in the "corners", but performance boost might be worth it.

Thanks in advance.

Jul 17 '05 #2
"Xenophobe" <xe*******@planetx.com> wrote in message
news:sSAZb.91266$jk2.439590@attbi_s53...
Using the zip code 10010, it takes almost 60 seconds to return 122
businesses within a 25 mile radius. This slowdown is almost entirely due to the query radius distance formula.

I wonder if someone knows the formula to calculate the distance within a
given square. Would this increase performance? I presume the results would
include zips in the "corners", but performance boost might be worth it.


I've got a system where I do something like this, but I precalculate the
distances. Whenever I update the ZIP database, I do something similar to the
following:

1) Calculate the distance (straight-line according to the particular formula
I use) between every pair of ZIP codes.

In my case, I'm only using ZIP codes for Long Island and the five boroughs,
so I've only got a few thousand, resulting in a manageable table. If you did
this for the entire national database, you're talking about LOTS of rows.

The table looks like this:

CREATE TABLE ZIPDistance (
ZIPCode1 DECIMAL(5),
ZIPCode2 DECIMAL(5),
Distance DECIMAL(3)
)

By convention, I always store the "lower" of the two ZIP codes in ZIPCode1.

2) Delete everything in the table where the distance is over my maximum
lookup distance.

You can now quickly get all of the businesses within a certain distance with
a query along these lines:

SELECT * FROM Business WHERE ZIPCode IN
(
SELECT ZIPCode2 FROM ZIPDistance WHERE ZIPCode1 = 11787 AND Distance <= 10
UNION
SELECT ZIPCode1 FROM ZIPDistance WHERE ZIPCode2 = 11787 AND Distance <= 10
UNION
SELECT 11787
)

Note: I'm running on SQL Server for this project, so I can write these sorts
of queries. You'll need to come up with a working method for MySQL. That is
left as an exercise for the interested student. :)

Steve
--
Steven C. Gallafent - The Computer Guy
st***@compguy.com - http://www.compguy.com/
Jul 17 '05 #3
Thank you for your reply.

I discovered the root of my problem was the lack of proper indexing. This
was THE reason for the poor performance, not the query radius calculation
math.

A 25 mile radius search around 10010 that took 60 seconds before, but now
only take 2 seconds.

The moral of this story is index, index, index.

"Chung Leong" <ch***********@hotmail.com> wrote in message
news:M_********************@comcast.com...
The trick is to use the box to quickly filter out the ones that can't
possibly satistfy the condition, before doing the more expensive radius
calculation.

First of all, add the latitude and longtitude of each business to the second table. Since these are going to stay the same through difference searches,
we can do the look up in advance. Be sure to put indexes on the columns.

Using the lat., long. of the zip code provide by the user, calculate the box
$left = $long - $dist; $right = $long + $dist;
$top = $lat + $dist; $bottom = $lat - $dist;

and use it in the query to exclude those businesses outside the box.

$result = mysql_query("SELECT * FROM business
WHERE ($left < long) AND (long < $right)
AND ($bottom < lat) AND (lat < $top);

Now, if you want, you can do a radius check one each row. It's probably
unnecessary though, since you don't actually know precise location of the
user and the businesses.
Uzytkownik "Xenophobe" <xe*******@planetx.com> napisal w wiadomosci
news:sSAZb.91266$jk2.439590@attbi_s53...
I have successfully created a zip code radius search, but the performance
is
unacceptable.

I have two tables. The first is 52K zip codes w/lat and long. The second

is
3K national business addresses.

Using the zip code 10010, it takes almost 60 seconds to return 122
businesses within a 25 mile radius. This slowdown is almost entirely due

to
the query radius distance formula.

I wonder if someone knows the formula to calculate the distance within a
given square. Would this increase performance? I presume the results

would include zips in the "corners", but performance boost might be worth it.

Thanks in advance.


Jul 17 '05 #4
Hi,

On Sat, 21 Feb 2004 04:10:00 GMT, "Xenophobe" <xe*******@planetx.com>
wrote:
I have successfully created a zip code radius search, but the performance is
unacceptable.

I have two tables. The first is 52K zip codes w/lat and long. The second is
3K national business addresses.

Using the zip code 10010, it takes almost 60 seconds to return 122
businesses within a 25 mile radius. This slowdown is almost entirely due to
the query radius distance formula.

What about the suggestion I made last time you asked? It would be
heaps faster, because it doesn't do any calculation

HTH, Jochen
--
Jochen Daum - Cabletalk Group Ltd.
PHP DB Edit Toolkit -- PHP scripts for building
database editing interfaces.
http://sourceforge.net/projects/phpdbedittk/
Jul 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
3862
by: Xenophobe | last post by:
I have successfully converted the ASP code included in the following article to PHP: http://www.4guysfromrolla.com/webtech/040100-1.shtml As described the high and low latitudes and longitudes...
7
10998
by: csumner | last post by:
I am trying to use the haversine function to find the distance between two points on a sphere, specifically two zip codes in my database. I'm neither horribly familiar with SQL syntax nor math...
9
3425
by: Sandy | last post by:
Hello - I need either a cheap tool or code & DB that calculates, eg. within 50-mile radius of a zip code. Anyone have any suggestions? -- Sandy
4
3839
by: Nick | last post by:
hi, guys I don't know where should I put this post, because this is a general question, not really a c# question. The question is how to caculate the real distance between two geographical...
4
1615
by: Dave | last post by:
Hi I want to add functionality to my site so that customers can type in a postcode and we can tell them the "nearest store" in our database. I have noticed there are loads of sites on the net...
1
4752
by: radskate360 | last post by:
Hi I am newer to programming and need a bit of help with this program. OK, heres the directions. The distance between two places on earth can be calculated by using their latitudes and...
1
1604
by: ccaddiso | last post by:
I want to retrieve my x1, x2, y1, and y2 from a text file using getline, but i don't know how. const double PI = 2.0 * asin(1.0); // class declaration class Point { protected:
1
4045
by: tiffrobe | last post by:
I'm a little lost on my program. Everything works fine except function 3. It gives out garbage numbers. Its suppose to give the distance between two points and then the area of 2 circles. ...
11
6355
by: devnew | last post by:
hello while trying to write a function that processes some numpy arrays and calculate euclidean distance ,i ended up with this code (though i used numpy ,i believe my problem has more to do with...
0
7067
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7264
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6975
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5562
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4992
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3160
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3148
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1495
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
371
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.