Well, if you really want closest as in "the fewest miles away", then you need to store some X,Y coordinates for every zip code (not just the ones currently in your database). Zip codes are organized such that ones that are numerically close are also fairly close physically but it is impossible to organize them such that those that are physically close are also always numerically close.
Anyway, once you have X,Y coordinates, efficiently finding the closest match can be tricky. One trick I developed long ago (which I'm curious if others have used since I've never run into it elsewhere) is to build a B-Tree key on int($X/$W),$Y (for example, on pack("NN",int($X/$W),$Y) or sprintf("%08d.%08d",$X/$W,$Y)) where $W is a "width" that is a little more than the distance you expect nearest matches to be from each other.
This sorts your data into bands of width $W. Then finding the closest match can be done with a few very fast queries. The exact algorythm is rather complex (if you really want to maximize the speed and if you can't guarantee a reasonable upper bound on the distance to the nearest match) but if you have a potentially huge number of points to search, this can be a big win.
- tye (but my friends call me "Tye")In reply to (tye)Re: Algorythym for searching closest neighbor
by tye
in thread Algorythym for searching closest neighbor
by belize
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |