我有一个MySQL例程,当通过纬度和经度使用Haversin方程时,在半径50英里范围内获得记录。
虽然这很好用,并且非常快速(考虑到它正在搜索82k记录),但我认为通过使用POINT列创建类似的过程可以获得更好的性能。
所以,在我的表中,我创建了一个名为Location
的额外列,给它一个POINT数据类型,更新了我的数据以传递lat& lon到Location
列。数据有效,并且很好。并添加了Spatial Index
问题是,如何转换以下查询以使用Location
列,而不是lat
和lon
列。
SET @LAT := '37.953';
SET @LON := '-105.688';
SELECT DISTINCT
BPZ.`store_id`,
3956 * 2 * ASIN(SQRT(POWER(SIN((@LAT - abs(Z.`lat`)) * pi()/180 / 2),2) + COS(@LAT * pi()/180 ) * COS(abs(Z.`lat`) * pi()/180) * POWER(SIN((@LON - Z.`lon`) * pi()/180 / 2), 2))) as distance,
c.`name`,c.`address`,c.`city`,c.`state`,c.`phone`,c.`zip`,c.`premise_type`
FROM
`zip_codes` as Z,
`brand_product_zip` as BPZ
LEFT JOIN `customers` c ON c.`store_id` = BPZ.`store_id`
WHERE
BPZ.`zip` = Z.`zip`
AND
3956 * 2 * ASIN(SQRT(POWER(SIN((@LAT - abs(Z.`lat`)) * pi()/180 / 2),2) + COS(@LAT * pi()/180 ) * COS(abs(Z.`lat`) * pi()/180) * POWER(SIN((@LON - Z.`lon`) * pi()/180 / 2), 2))) <= 50
ORDER BY
distance LIMIT 20
我知道之前已经提到过,但是,我看到的所有内容都指向基于lat
和lon
而不是POINT
列的计算
更新代码:
SET @lat = 41.92;
SET @lon = -72.65;
SET @kmRange = 80.4672; -- = 50 Miles
SELECT *, (3956 * 2 * ASIN(SQRT(POWER(SIN((@lat - abs(`lat`)) * pi()/180 / 2),2) + COS(@lat * pi()/180 ) * COS(abs(`lat`) * pi()/180) * POWER(SIN((lon - `lon`) * pi()/180 / 2), 2)))) as distance
FROM `zip_codes`
WHERE MBRContains(LineString(Point(@lat + @kmRange / 111.1, @lon + @kmRange / (111.1 / COS(RADIANS(@lat)))), Point(@lat - @kmRange / 111.1, @lon - @kmRange / (111.1 / COS(RADIANS(@lat))))), `Location`)
Order By distance
LIMIT 20
答案 0 :(得分:2)
您是否研究过希尔伯特曲线解决方案?空间索引无法提供准确的解决方案? 。使用mysql空间索引,您可以使用mbrcontains:
CREATE TABLE lastcrawl (id INT NOT NULL PRIMARY KEY, pnt POINT NOT NULL) ENGINE=MyISAM;
INSERT
INTO lastcrawl
VALUES (1, POINT(40, -100));
SET @lat = 40;
SET @lon = -100;
SELECT *
FROM lastcrawl
WHERE MBRContains
(
LineString
(
Point
(
@lat + 10 / 111.1,
@lon + 10 / ( 111.1 / COS(RADIANS(@lat)))
),
Point (
@lat - 10 / 111.1,
@lon - 10 / ( 111.1 / COS(RADIANS(@lat)))
)
),
pnt
);
请看这里:MySQL - selecting near a spatial point。 这里:http://www.drdobbs.com/database/space-filling-curves-in-geospatial-appli/184410998
答案 1 :(得分:1)
文章Nearest-location finder for MySQL详细解释了各种选项,以及与Spatial Extensions starting with MySQL 5.6一起使用的最佳选择。
从文章中,此示例查询列出了距离给定坐标(42.81,-70.81)半径50英里范围内的邮政编码:
SELECT zip, primary_city,
latitude, longitude, distance_in_mi
FROM (
SELECT zip, primary_city, latitude, longitude,r,
69.0 * DEGREES(ACOS(COS(RADIANS(latpoint))
* COS(RADIANS(latitude))
* COS(RADIANS(longpoint) - RADIANS(longitude))
+ SIN(RADIANS(latpoint))
* SIN(RADIANS(latitude)))) AS distance_in_mi
FROM zip
JOIN (
SELECT 42.81 AS latpoint, -70.81 AS longpoint, 50.0 AS r
) AS p
WHERE latitude
BETWEEN latpoint - (r / 69)
AND latpoint + (r / 69)
AND longitude
BETWEEN longpoint - (r / (69 * COS(RADIANS(latpoint))))
AND longpoint + (r / (69 * COS(RADIANS(latpoint))))
) d
WHERE distance_in_mi <= r
ORDER BY distance_in_mi;