The database has a table with all coordinates of the Moscow metro (id, name, lat, lng) - lat and lng are latitude and longitude.
Also in the database there is another table, e.g. table organizations, it has a lot of fields, and also have lat and lng fields (the coordinates of this organization)
To get all subway in a radius of 2000 meters from the organization.
And here is the question: which of the solutions is better in performance?
From the computed entry in the radius directly in the sql query:
example written for laravel, knowing I understand
$metro = \App\Metro::select(\DB::raw('*, (
6378137 * acos (
cos ( radians(' . $res['lat'] . ') )
* cos( radians( lat ) )
* cos( radians( lng ) - radians(' . $res['lng'] . ') )
+ sin ( radians(' . $res['lat'] . ') )
* sin( radians( lat ) )
) AS distance'))
->having('distance', '<=', $res['radius_metro'])
Under this option, we will always have a fresh result, for example if we add a new subway station.
To bring the search result to the nearest metro station in a separate table and periodically update it via cron for example.
Now interested in what is the best option to use, I like the 1st.
Planned heavily loaded portal