我想优化我的表以加快查询结果。目前我正在使用 MyISAM 而不是 InnoDB。
问题是,第一个邮政编码是 5 个字符长并且不以A字母开头。所有以A字母开头的邮政编码都是 6 或 7 个字符长,大约位于表格记录的中间。您认为我的表中的记录应该按 AZ 顺序排列还是按邮政编码的长度(表顶部列出的字符较少的那些)更适合优化?
或者你还有什么建议?
表结构:
CREATE TABLE `postcodes` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`postcode` varchar(10) NOT NULL,
`latitude` decimal(25,20) NOT NULL,
`longitude` decimal(25,20) NOT NULL,
`fk_areas_id` int(11) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `unique_postcodes_postcode` (`postcode`) USING HASH
) ENGINE=MyISAM AUTO_INCREMENT=1696089 DEFAULT CHARSET=utf8;
询问:
SELECT
(@rownum := @rownum + 1) AS No,
postcode AS Postcode,
latitude AS Latitude,
longitude AS Longitude,
(
(
ACOS(SIN((SELECT latitude FROM postcodes WHERE postcode = $postcode) * PI() / 180) * SIN(latitude * PI() / 180) +
COS((SELECT latitude FROM postcodes WHERE postcode = $postcode) * PI() / 180) * COS(latitude * PI() / 180) *
COS(((SELECT longitude FROM postcodes WHERE postcode = $postcode) - longitude) * PI() / 180)) * 180 / PI()
) * 60 * 1.1515
) AS Distance
FROM postcodes, (SELECT @rownum := 0) AS No
HAVING Distance <= 0.5 /*miles*/
ORDER BY Distance ASC