How to optimize a SQL query with calculating the distance in longitude and latitude?

I have a table with a structure like this:

table name: shop

id_shop      int(10)
name         varchar(200)
latitude     double
longitude    double

And I would like to calculate the distance between the given coordinates and the coordinates stored in the database.

My current request:

SELECT *
  FROM `shop` AS `s`
 WHERE
      (
        ( 6371
        * ACOS(
            SIN( RADIANS( latitude ) )
          * SIN( RADIANS( 53.5353010379 ) )
          + COS( RADIANS( latitude ) )
          * COS( RADIANS( 53.5353010379 ) )
          * COS( RADIANS( 14.7984442616 ) - RADIANS( longitude ) )
          )
        )
        <= 25
      )

plus some JOIN LEFTfor some data.

Is there any way to optimize this query? With joins, it takes about 13 ms.

I need to add here some LIMIT, and COUNT(*)for the total number of stores for pagination.

+3
source share
2 answers

Here are a few ideas, some of which may not apply depending on your specific situation.

  • . (, ).
  • , , Haversince, , Haversince.
  • , (// ..), .
  • , , , .
+5

, . , , RADIANS () RADIANS (), , , ( , .)

, , SIN (RADIANS ()) COS (RADIANS ()), ...

, , " X" - , , , - , , , .

+5

Source: https://habr.com/ru/post/1751216/


All Articles