XML 搜索或 DB 搜索 / javascript(客户端)或 php(服务器端)计算
假设您的网站每天有 200,000 个唯一用户。所以,你的服务器负载很重;而且您没有资源购买更大/更好的服务器。所以,你被你所拥有的所困。
现在,每当用户访问您的网站时,您都需要进行一些计算(计算通过 GeoIP 检测到的用户城市与一些白名单城市之间的距离,找出 140 英里半径内最近的城市)。
您会通过 PHP 还是 JavaScript 进行此计算?
首先,您会预先计算白名单城市 140 英里半径范围内的所有附近城市吗?例如:白名单城市1可以有20个附近的城市。或者你会每次都进行即时计算吗?
例如: 白名单 = 密歇根州底特律 附近的城市 = 卡拉马祖,密歇根州(140 英里)
第二,如果预先计算:您会将其存储在 XML 文件或某个 MySQL 表中?现在,我们只需搜索一个表(mysql 或 xml 大小不超过 1 MB)。我猜这效率很低,因为客户端浏览器 (JavaScript) 必须下载 1mb xml 并搜索它。这会使页面加载时间更慢。使用数据库可能会更快,但数据库负载会增加(如果一天中有 200,000 个唯一用户尝试加载页面)。
也许最好的方法是进行预计算,将预计算结果存储在 XML 中,然后使用 PHP 搜索 XML 并找到最近的用户的白名单城市?
Let's say your site has 200,000 unique users a day. So, your server is heavily loaded/pounded; and you do NOT have resources to buy a bigger/better server. So, you are stuck with what you have.
Now, whenever a user comes to your site, you need to do some calculation (calculate distance between user city as detected via GeoIP and some whitelist of cities, figure out the nearest city within 140 mile radius).
Would you do this calculation via PHP or via JavaScript?
First, would you precalculate all nearby cities within 140 mile radius of whitelisted cities? For eg: Whitelist city 1 can have 20 nearby cities. Or would you do on-the-fly calculation everytime?
For eg:
Whitelist = Detroit, MI
and nearby city = Kalamazoo, MI (140 miles)
Second, if pre-computed: would you store this in XML file or some MySQL table? Now, we just have to search through a table (mysql or xml no more than 1 mb in size). I am guessing this would be inefficient because client browser (JavaScript) would have to download 1mb xml and search through it. This would make page load time even slower. Using DB might be faster but then DB load increases (if 200,000 unique users are trying to load the page over the course of a day).
Maybe the best way to do would be to do precompute, store precomputed results in XML, and then use PHP to search through XML and find nearest whitelisted city to user?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您(站点)实际上依赖于城市信息,那么您必须在服务器上进行计算。
对于足够大的 XML 文件,数据库查询几乎总是比 XML 搜索更快。你可以优化查询,MySQL 会缓存一些东西,等等。
预先计算所有城市到城市的距离肯定是一种方法。 GeoIP 不仅提供城市名称,还提供实际的纬度/经度位置。我确信可能的城市列表也会不断变化。
我会考虑使用 MySQL 的地理空间功能。按坐标搜索的一般概述:
查找两个纬度/经度点之间距离的最快方法
简而言之,您要做的就是设置您关心的城市及其经纬度的数据库,并根据 GeoIP 提供的纬度/经度查询该表。
If you, the site, are actually relying on the city information, then you must do the calculation on the server.
Database queries are almost always going to be faster than XML searches for sufficiently large XML files. You can optimize the query, MySQL will cache things, etc.
Pre-calculating all city-city distances would be a way to go, for sure. GeoIP doesn't only provide city names, it does give actual latitude/longitude locations as well. I'm sure that the possible list of cities changes rather constantly, too.
I would look into using the geospacial capabilities of MySQL. General over view of searching by coordinates here:
Fastest Way to Find Distance Between Two Lat/Long Points
In short what you will do is setup a database of the cities you care about, with their lat/long, and query that table based on the GeoIP provided lat/long.