views:

46

answers:

2

Hello !

I have a table with structure like that:

table name: shop

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

And I'd like to calculate distance between given coordinates and coordinates saved in database.

My current query:

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 LEFT's for some data.

Is there any way to optimize that query ? With joins it takes about 13msec.

I need to add here also some LIMIT and COUNT(*) for total amount of shops for pagination.

+2  A: 

Well, for starters, you could store pre-calculated stuff in the database when you store the latitude and longitude. If you pre-store the latitude and longitude as radians, for example, you'll only need to calculate RADIANS(latitude) and RADIANS(longitude) once, when you store each location, not every time you need to do your distance calculation (presumably far more than once.)

Maybe shave off a bit more by storing the SIN(RADIANS(latitude)) and COS(RADIANS(latitude)) when you first populate the row, too...

I'm guessing you're doing many, many "nearest thing to X" calculations over time -- that's what people are generally doing when faced with this calculation -- and pre-calculating what you can is normally the first thing to try.

Matt Gibson
+1  A: 

Here's a few ideas, some of which may not apply depending on your exact situation.

  1. You could do the conversion of latitude and longitude to radians and store that in the row as well. This would save the cost of those calculations (actually the cost would be accrued once when storing the data).
  2. If your table is very large, you could use a simple linear distance calculation rather than the Haversince formula to limit the results to which you apply the Haversince formula.
  3. If you have other data in the table that would serve as a good first filter (country/region/etc.), you could apply it first.
  4. You could reorder your joins so that they are applied after the distance filter so that you don't incur the cost of the join on data that doesn't qualify.
tvanfosson
1. Nice idea - I should think about that; 2. Yes, table is large. How should that linear calculation look like ? 3. I do not have that data; 4. It did not change anything.
hsz
2. Basically, you can just use Pythagoras's theorem to calculate the distance -- this can work "well enough" depending on your data and what you're doing, it's fine for a fairly limited area of the planet earth when you're just looking for "nearest thing" rather than actually needing an accurate distance. You can also do things like use Pythagoras to do a rough calculation to narrow down your dataset then do the more accurate, costly Haversine calculation on your narrowed-down, smaller dataset.
Matt Gibson