优化查询(2个坐标之间的距离)

时间:2014-07-13 14:58:14

标签: php mysql sql database-administration

我有一个很大的POI数据库,现在大约150K,但会增长到~1M

我需要返回某一点半径内的所有点(lat,lon) 我有这个查询,直到行数增加,效果很好。 此查询使用大约60%的CPU,基本上超级慢

SELECT 
    *,
    (((acos(sin((57.757134*pi()/180)) * 
                    sin((coX*pi()/180))+cos((57.757134*pi()/180)) * 
                    cos((coX*pi()/180)) * cos(((37.616015- coY)* 
                    pi()/180))))*180/pi())*60*1.1515
                    ) as distance 
FROM 
    places_en
JOIN (   /* these are the query parameters */
      SELECT  
          57.757134  AS latpoint, 37.616015 AS longpoint,
          200 AS radius, 111.045 AS distance_unit) AS p
WHERE 
     description != ''
     AND coX BETWEEN p.latpoint  - (p.radius / p.distance_unit)
                 AND p.latpoint  + (p.radius / p.distance_unit)
     AND coY BETWEEN p.longpoint - (p.radius / (p.distance_unit * COS(RADIANS(p.latpoint))))
                 AND p.longpoint + (p.radius / (p.distance_unit * COS(RADIANS(p.latpoint))))
HAVING 
    distance <= 200  
ORDER BY 
    distance;

如何让它变得更好?

由于

1 个答案:

答案 0 :(得分:0)

如果有人会在这里碰到这个,那就是灾难:

我已经设置了一个专栏&#34; geoPoint&#34;作为POINT类型。

   SELECT
            *,
            (GLength(
            LineStringFromWKB(
              LineString(
                geoPoint, 
                GeomFromText('POINT(".$lat." ".$lon.")')
              )
             )
            ))
            AS distance
            FROM TABLE_NAME
            HAVING distance <= ".$radius."
            ORDER BY distance