PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Logiciels d'hébergement > mailing.database.mysql > smarter zipcode search algorithm
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
smarter zipcode search algorithm

Réponse
 
LinkBack Outils de la discussion
Vieux 06/07/2006, 00h30   #1
premgrps@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut smarter zipcode search algorithm

Hi,
I have a database with two tables
a) A table of 2 million records with city, zip and associated
information (say XYZ) and
b) zipcode latitude, longitude table having >40,000 records/zip codes

PROBLEM:
I need to find the the XYZs within the the range of a certain zipcode.
This zipcode and radial range in miles is entered by the user (web
interface).

The brute force way is to calculate the distance between the user
zipcode and all the zipcodes in the database. Once the zipcode_range
subroutine gives back the zipcodes within a certain radius, I need to
find all the XYZs from the table #1.

Another approach is to find the zipcodes with a square region (min/max
of the user zipcode latitude/longitude position).

Both the approaches are consuming too much time. Especially if the
radial distance starts increasing.

My questions:
1. Is there any other smart way to do the above task.
2. I am working on a 2.4ghz/512MB RAM machine. Any suggestions how to
increase the performance. Right now each select command to the
2Million record table takes about a minute.

Thanks.

  Réponse avec citation
Vieux 06/07/2006, 14h36   #2
zac.carey@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: smarter zipcode search algorithm

premgrps@gmail.com wrote:
> Hi,
> I have a database with two tables
> a) A table of 2 million records with city, zip and associated
> information (say XYZ) and
> b) zipcode latitude, longitude table having >40,000 records/zip codes
>
> PROBLEM:
> I need to find the the XYZs within the the range of a certain zipcode.
> This zipcode and radial range in miles is entered by the user (web
> interface).
>
> The brute force way is to calculate the distance between the user
> zipcode and all the zipcodes in the database. Once the zipcode_range
> subroutine gives back the zipcodes within a certain radius, I need to
> find all the XYZs from the table #1.
>
> Another approach is to find the zipcodes with a square region (min/max
> of the user zipcode latitude/longitude position).
>
> Both the approaches are consuming too much time. Especially if the
> radial distance starts increasing.
>
> My questions:
> 1. Is there any other smart way to do the above task.
> 2. I am working on a 2.4ghz/512MB RAM machine. Any suggestions how to
> increase the performance. Right now each select command to the
> 2Million record table takes about a minute.
>
> Thanks.


have you read this article over at
http://www.phparchitect.com/sample.php?mid=9

the whole thing seems very informative - particularly listing 3

  Réponse avec citation
Vieux 10/07/2006, 16h07   #3
Jeff
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: smarter zipcode search algorithm

zac.carey@gmail.com wrote:
> premgrps@gmail.com wrote:
>
>>Hi,
>> I have a database with two tables
>>a) A table of 2 million records with city, zip and associated
>>information (say XYZ) and
>>b) zipcode latitude, longitude table having >40,000 records/zip codes
>>
>>PROBLEM:
>>I need to find the the XYZs within the the range of a certain zipcode.
>>This zipcode and radial range in miles is entered by the user (web
>>interface).
>>
>>The brute force way is to calculate the distance between the user
>>zipcode and all the zipcodes in the database. Once the zipcode_range
>>subroutine gives back the zipcodes within a certain radius, I need to
>>find all the XYZs from the table #1.
>>
>>Another approach is to find the zipcodes with a square region (min/max
>>of the user zipcode latitude/longitude position).
>>
>>Both the approaches are consuming too much time. Especially if the
>>radial distance starts increasing.
>>
>>My questions:
>>1. Is there any other smart way to do the above task.
>>2. I am working on a 2.4ghz/512MB RAM machine. Any suggestions how to
>>increase the performance. Right now each select command to the
>>2Million record table takes about a minute.
>>
>>Thanks.

>
>
> have you read this article over at
> http://www.phparchitect.com/sample.php?mid=9
>
> the whole thing seems very informative - particularly listing 3


Seems about right to me.

I've tried the cos, sin acos and that seemed a little slow so I did this:

(Perl)


my
$mile_lat=convertToMiles($L{latitude},$L{longitude },$L{latitude}-1,$L{longitude});
my
$mile_lon=convertToMiles($L{latitude},$L{longitude },$L{latitude},$L{longitude}-1);

$sql_zip=qq{ , ceiling(sqrt(pow(((latitude - $L{latitude}) *
$mile_lat),2) + pow(((longitude -$L{longitude}) * $mile_lon),2))) AS
distance };

sub convertToMiles{
($lat1, $lon1, $lat2, $lon2)=@_;
$dist = acos(sin(deg2rad($lat1)) * sin(deg2rad($lat2)) +
cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($lon1 - $lon2)));

$dist = rad2deg($dist);
return $miles = $dist * 69;
} # end sub

The $L{latitude} stuff is from the zipcode database, I found that
indexing the zipcode database was essential!

Close enough and more than fast enough my purposes. All that is doing is
finding the approximate distance per degree ($mile_lat and $mile_lon)
and then just finding the length of the hypoteneus. That will start to
fail for large distances (multi state) because the earth is curved.

Jeff




>

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 07h10.


Édité par : vBulletin® version 3.7.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,10047 seconds with 11 queries