如何加快“最近记录” mySQL查询的速度

时间:2019-03-15 09:00:50

标签: mysql

我正在尝试查看是否可以通过索引(或其他调整)来加快此查询的速度。基本上,我想在glinks_Links表中根据纬度经度查找6个最接近的记录。

SELECT
    ID, Ville, Title, Telephone, ExpiryDate,
    Address, Postal_Code, Latitude, Longitude, priceSort, priceSortBNB, priceSortGITE, has_image,

    ( 6371 * acos( cos( radians('42.934817491341060000000000000000') ) * cos( radians( Latitude ) ) * cos( radians( Longitude ) - radians('2.222607135772705000000000000000') ) + sin( radians('42.934817491341060000000000000000') ) * sin( radians( Latitude ) ) ) ) AS distance
    FROM glinks_Links link
        WHERE
            link.ExpiryDate < 2147483647
            AND isValidated = "Yes"
            AND OffersLinkID IS NULL
            AND (
            ((Latitude BETWEEN (42.934817491341060000000000000000 - 35/69.0) AND (42.934817491341060000000000000000 + 35/69.0)) )
            AND
            (Longitude BETWEEN (2.222607135772705000000000000000 - 35/42.5) AND (2.222607135772705000000000000000 + 35/42.5))
            )
        HAVING
            distance < 25
        ORDER BY distance
        LIMIT 6

表结构(为简单起见,删除了所有不在查询中的多余内容):

CREATE TABLE `glinks_Links` (
  `ID` int(10) UNSIGNED NOT NULL,
  `Title` varchar(100) NOT NULL DEFAULT '',
  `isValidated` enum('No','Yes') NOT NULL DEFAULT 'Yes',
  `ExpiryDate` int(11) NOT NULL DEFAULT '2147483647',
  `Address` varchar(100) DEFAULT NULL,
  `Postal_Code` varchar(30) DEFAULT NULL,
  `Ville` varchar(50) DEFAULT NULL,
  `Latitude` decimal(34,30) DEFAULT NULL,
  `Longitude` decimal(34,30) DEFAULT NULL,
  `OffersLinkID` int(11) DEFAULT NULL,
  `priceSortBNB` int(11) DEFAULT NULL,
  `priceSortGITE` int(11) DEFAULT NULL,
  `priceSort` int(11) DEFAULT NULL,
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

运行仅需0.03秒,但我必须做50k左右的时间,所以所有这些加起来。任何建议都非常感谢:)

更新:我可能应该提到我已经在表上有一些索引:

ALTER TABLE `glinks_Links`
  ADD PRIMARY KEY (`ID`),
  ADD KEY `expiryndx` (`ExpiryDate`),
  ADD KEY `valexpndx` (`isValidated`,`ExpiryDate`),
  ADD KEY `Latitude` (`Latitude`,`Longitude`);

0 个答案:

没有答案