简体   繁体   中英

Combining SQL results into groups based on column value

I have a table with over 2 million rows. One of the values is an address. Some rows have a common address. I am using php.

On my website, I want the user to put in their zip code, and I will return all results within that zip code. I would then use Google to Geolocate them on a map. The problem is that since Google charges by the query, I can't be wasting time and money requesting coordinates for an address I already have. Here is what I believe to be the correct approach:

  1. Ask user for zip code

  2. Run "Select * with 'Zip Code' = $user_zip" (paraphrasing)

  3. Run a Geolocate on first address and plot on map

  4. Check for matching addresses in result and group with the mapped result

  5. Find next new address

  6. Repeat 3-6 until complete

Is there a better way to approach this? I am looking for efficiency, easy way to manipulate all matching results at once, and the least amount of queries. If my way is correct, can someone please help me with the logic for numbers 3-5?

If I understand this right what you are trying to do is to render a map with markers for each record in your database that is within a certain zip area. And your challenge is that you need coordinates to render each marker. The biggest issue with your approach in terms of wasting resources is that you do not store the coordinates of each address in your database. I would suggest you to:

1 - Alter the endpoint (or script or whatever) that creates these records in your db to fetch the coordinates and store them in the database. 2 - Run a one time migration to fetch coordinates for each record. While I understand that doing this for 2 milion rows could be "costly" with Google's Geocoding (Estimate is 1000$ for 2 milion api calls). To save the costs you could look into some of the opensource map tools.

Either way fetching coordinates during the request lifecycle is both a waste of resource and it will significantly affect speeds.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM