title: Calculating distance between Geo points for large datasets
body: <p>I'm building an online Symfony application, and as part of the developement process i've been tasked with sorting an amount of database records based on the distance from the logged user; said user can widen the search radius at will, up to the size of the whole world.</p>
  
  <p>At any moment i have access to the GPS coordinates of the logged user, and on a database table i saved latitude and longitude of the various points of interest.</p>
  
  <p>Currently, there are only 400 records in the POIs' table, but due to the amount of data i must extract whenever i access it, the query time is already slightly over a second. Adding 400 trigonometric functions to such workload will soon bring such execution time beyond acceptability.</p>
  
  <p>I thus need a method that's both fast and accurate to calculate such distances;</p>
  
  <p>I've read multiple articles suggesting the Haversine formula, but i found that to be too slow for my needs and even an extensive article like <a href="http://www.movable-type.co.uk/scripts/latlong.html" rel="nofollow noreferrer">this</a> couldn't be of any help;</p>
  
  <p>Considering that i could soon reach thousands of POIs with thousands of users logged at the same time from all over the world, how could i approach (and hopefully solve) such problem?</p>
  
  <p>I'm using PHP 7.0, Symfony 3.2, and Doctrine; pdo to interface to a Mysql server, with innoDB as the database engine<br>
  My customer values accuracy over speed, but can't stand to wait more than 5 seconds<br>
  The query results are paged, thus delegating the sorting to the client is impossible<br>
  Both the database and the php server share the same (terrible) resource pool, and such pool is to be shared with other applications</p>
  
  <p>On a sidenote, some of the POIs may expire after a certain date</p>