开发者

XML search or DB search / javascript (client side) or php (server side) calculation

开发者 https://www.devze.com 2023-04-08 08:26 出处:网络
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.

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 di开发者_如何转开发stance 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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号