我使用了这个查询,mysql 给出结果的时间太长了。行数为 160 万。
SELECT DISTINCT TB.ID, Latitude, Longitude,
111151.29341326*SQRT(pow(-6-`Latitude`,2)
+pow(106-`Longitude`,2)*cos(-6*0.017453292519943)
*cos(`Latitude`*0.017453292519943)) as Distance
FROM `tablebusiness` AS TB, `tablecity` AS TC, `businessestag` AS BC,
`businessesdistricts` AS BD, `tabledistrict` AS TD,
(SELECT ID,
(SELECT Title
FROM `tablebusiness` As TBuild
WHERE TBuild.ID = TBB.Building) As BuildingTitle
FROM `tablebusiness` As TBB) AS TBuilding
WHERE TB.City = TC.City AND BC.BusinessID = TB.ID AND BD.BusinessID = TB.ID
AND TD.ID = BD.District AND TBuilding.ID = TB.ID
AND (`Title` LIKE '%%' OR `Street` LIKE '%%' OR TB.City LIKE '%%'
OR Country LIKE '%%' OR Tag LIKE '%%' OR TD.District LIKE '%%'
OR TBuilding.BuildingTitle LIKE '%%')
AND (-6.0917668133836 < `Latitude` AND `Latitude` < -5.9082331866164
AND 105.90823318662 < `Longitude` AND `Longitude` < 106.09176681338)
ORDER BY Distance LIMIT 0, 100
即使行数是查询的 160 万部分,即
AND (-6.0917668133836 < `Latitude` AND `Latitude` < -5.9082331866164 AND 105.90823318662 < `Longitude` AND `Longitude` < 106.09176681338)
大大限制了行数。似乎mysql不会通过搜索来优化查询
AND (-6.0917668133836 < `Latitude` AND `Latitude` < -5.9082331866164 AND 105.90823318662 < `Longitude` AND `Longitude` < 106.09176681338) first.
我通过移动前面的纬度经度方面来更改查询
SELECT DISTINCT TB.ID, Latitude, Longitude,
111151.29341326*SQRT(pow(-6-`Latitude`,2)
+pow(106-`Longitude`,2)*cos(-6*0.017453292519943)
*cos(`Latitude`*0.017453292519943)) as Distance
FROM `tablebusiness` AS TB, `tablecity` AS TC, `businessestag` AS BC,
`businessesdistricts` AS BD, `tabledistrict` AS TD,
(SELECT ID,
(SELECT Title
FROM `tablebusiness` As TBuild
WHERE TBuild.ID = TBB.Building) As BuildingTitle
FROM `tablebusiness` As TBB) AS TBuilding
WHERE (-6.0917668133836 < `Latitude` AND `Latitude` < -5.9082331866164
AND 105.90823318662 < `Longitude` AND `Longitude` < 106.09176681338)
AND TB.City = TC.City AND BC.BusinessID = TB.ID AND BD.BusinessID = TB.ID
AND TD.ID = BD.District AND TBuilding.ID = TB.ID
AND (`Title` LIKE '%%' OR `Street` LIKE '%%' OR TB.City LIKE '%%'
OR Country LIKE '%%' OR Tag LIKE '%%' OR TD.District LIKE '%%'
OR TBuilding.BuildingTitle LIKE '%%')
ORDER BY Distance LIMIT 0 ,100
它仍然一样慢。
那我该怎么办?