How to use a single mysql query instead of a query inside an array loop?

The request below contains a loop (in PHP) that takes latitude, longitude, and input from three different arrays. For each position / key in the array, the nearest geographical point is selected from the table using the following query.

Since the size of the array is usually around 500, so 500 sets of inputs, for which you need to find the closest geographical location, take some time.

My question is: is there a way to use a single request using input arrays from php right away and select a set of all the nearest geographical locations for each array key?

$sql="SELECT 
id, d, tolon, tolat, du,
       ( 3959 * acos( cos( radians($lat1) ) 
              * cos( radians( travel.tolat ) ) 
              * cos( radians( travel.tolon) - radians($lon1) ) 
              + sin( radians($lat1) ) 
              * sin( radians( travel.tolat ) ) ) ) AS distance 
FROM travel 
WHERE userid = $id AND tolon > $lonlow AND tolon < $lonhigh AND tolat > $latlow AND tolat < $lathigh
having distance < $maxdistance ORDER BY distance
";
+3
source share
1 answer

, , , limit 1 - , . sql, (max packet size - , ). , , 100 .

, , . mysql spatial indexes, , .

+1

All Articles