I have a database of information organized by US zipcode. I am building algorithms that crawls along adjacent zipcodes to determine the size of a 'city' based on density, job characteristics, or whatever. I use the location and area of any zip code to estimate which other zipcodes are adjacent to it. I am becoming aware that this algorithm is eating up most of my processing time when I run test of my program.
So what I want to do is have the map (as in a data structure map) of adjacent zipcodes in a table in my database.
My current implementation is that I have a table with two fields, source and target. Each time my algorithm determines two zipcodes are adjacent, the two codes are inserted both way into the table, as so:
+-----------+------------+
| source | target |
+-----------+------------+
| 02139 | 02138 |
| 02138 | 02139 |
+-----------+------------+
That way I can search for all adjacent zip codes with
SELECT target FROM adjacent WHERE source = '02139';
and get all the zip codes that are adjacent to '02139'.
Now strictly speaking, my implementation is just fine. For a set of less than 50,000 total zip codes, doing it the way I did doesn't really impose any computational penalties. However, not being indexed, and having each relationship inserted twice seems non-optimal, and since I'm just doing this for funzies and learning, I should put in the effort to optimize. So I'm trying to find out how to more efficiently simulate a mapping using a mysql table.
So the question is: what is the most efficient way to represent 1-to-n mappings using MySQL?