【发布时间】:2012-10-17 12:52:00
【问题描述】:
我有一张 Postcode 表格,其中包含所有英国邮政编码(我认为大约 180 万)
CREATE TABLE `Postcode` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`Postcode` varchar(8) DEFAULT NULL,
`Postcode_Simple` varchar(8) DEFAULT NULL,
`Positional_Quality_Indicator` int(11) DEFAULT NULL,
`Eastings` int(11) DEFAULT NULL,
`Northings` int(11) DEFAULT NULL,
`Latitude` double DEFAULT NULL,
`Longitude` double DEFAULT NULL,
`LatLong` point DEFAULT NULL,
PRIMARY KEY (`ID`),
UNIQUE KEY `Postcode` (`Postcode`),
KEY `Postcode_Simple` (`Postcode_Simple`),
KEY `LatLong` (`LatLong`(25))
) ENGINE=InnoDB AUTO_INCREMENT=1755933 DEFAULT CHARSET=latin1;
我想要实现的是...给定一个坐标,找到离坐标最近的邮政编码。问题是我为执行此操作而编写的查询(实际上是在存储过程中)遇到了一些问题。查询是:
SELECT
Postcode
FROM
(SELECT
Postcode,
GLENGTH(
LINESTRINGFROMWKB(
LINESTRING(
LatLong,
GEOMFROMTEXT(CONCAT('POINT(', varLatitude, ' ', varLongitude, ')'))
)
)
) AS distance
FROM
Postcode
WHERE
NOT LatLong IS NULL) P
ORDER BY
Distance
LIMIT
1;
我遇到的问题是查询需要大约 12 秒才能运行,而且我不能花那么长时间才能得到结果。谁能想到我可以可靠地加快这个查询的任何方法?
(这里是查询的解释)
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY <derived2> ALL (NULL) (NULL) (NULL) (NULL) 1688034 Using filesort
2 DERIVED Postcode ALL LatLong (NULL) (NULL) (NULL) 1717998 Using where
我一直在想办法缩小我必须执行距离计算的初始数据量,但我无法想出任何不限于查找邮政编码的方法在给定的距离内。
【问题讨论】:
-
你创建了索引,PKs,表结构是什么?加快查询速度
-
@jcho360 索引和主键已经创建,但不利于为我正在使用的查询创建交易。表结构已经如上图...
标签: mysql latitude-longitude spatial postal-code