Parvesh Parvesh - 2 months ago 11
Java Question

Calculating distance - on the fly or as soon as data is entered into database?

Working on a small web application, I have a user table in which user details is stored along with latitude and longitude of their current locations.

A new user registers, he is shown the distance of all the users from where he currently is.

I am doing it using the haversine formula.

My problem is how do I proceed on doing this?
How can I do this on the fly? Or do I need to calculate all the user distances when the user registers and save it?

Should cater for sorting: i.e shortest distance first.


If the number of users is going to be very large (or unbounded) then you may run into trouble.

Calculating this up-front might require quite a bit of storage. But calculating on demand might require a large amount of server CPU time.

Some considerations:

  1. Use MySQL to do the geo-sorting.

  2. Consider calculating distances for only a small subset of other users.

  3. Consider the benefits of having the distances calculated dynamically on the client machine using javascript.