我有一个表Postcode包含所有英国邮政编码(大约 1.8mi 认为)
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
我一直在想办法缩小我必须执行距离计算的初始数据量,但我无法想出任何不限于在给定范围内查找邮政编码的方法距离。