简体   繁体   English

根据列值将SQL结果分组

[英]Combining SQL results into groups based on column value

I have a table with over 2 million rows. 我有一个超过200万行的表。 One of the values is an address. 值之一是地址。 Some rows have a common address. 有些行有一个公共地址。 I am using php. 我正在使用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. 然后,我将使用Google在地图上对它们进行地理定位。 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. 问题在于,由于Google会根据查询收费,因此我不会浪费时间和金钱来请求已经拥有的地址的坐标。 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) 运行“ Select * with'Zip Code'= $ user_zip”(措辞)

  3. Run a Geolocate on first address and plot on map 在第一个地址上运行Geolocate并在地图上绘图

  4. Check for matching addresses in result and group with the mapped result 检查结果中是否匹配地址,并与映射结果分组

  5. Find next new address 查找下一个新地址

  6. Repeat 3-6 until complete 重复3-6直到完成

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? 如果我的方法是正确的,有人可以帮助我说明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. 如果我理解正确,那么您想要做的就是为带有标记的地图绘制一个映射,该标记用于数据库中某个zip区域内的每个记录。 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. 1-更改在数据库中创建这些记录的端点(或脚本或其他内容)以获取坐标并将其存储在数据库中。 2 - Run a one time migration to fetch coordinates for each record. 2-运行一次迁移以获取每个记录的坐标。 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). 虽然我了解使用Google的地理编码在200万行中执行此操作可能会“成本很高”(200万个api调用的估算费用为1000美元)。 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. 无论哪种方式,在请求生命周期中获取坐标都将浪费资源,并且会显着影响速度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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