2014-02-19 78 views
0

我使用php和mysql來查詢2G的數據,對於個人賬戶geogrpahy的數據總結了這個反對長和經緯度,然後我使用結果發送給谷歌熱圖我該如何提高我的地理位置查詢,以免花費8分鐘的CPU時間

這需要年齡......多達8分鐘爲計算

SELECT SUM(`Geography`.`Orders`)AS `weight`, 
     `location`.`latitude`, 
     `location`.`longitude` 
FROM `mobile`.`Geography` 
INNER JOIN `mobile`.`location` ON (`Geography`.`City` = `location`.`city`) 
AND (`Geography`.`Country` = `location`.`country`) 
WHERE (`Geography`.`Advertiser ID` = $mid) 
GROUP BY `Geography`.`City`; 

是否有這樣或替代一起做這更好的辦法? $ mid是用於替代賬戶信息的php varialbe

+0

是你的表建立索引? –

回答

1

您應該創建以下索引:Geography(AdvertiserId, City, Country, Orders)Location(city, latitude, longitude)

0

嘗試以下查詢:

select sum(g.Orders) as weight, 
    (select l.latitude from mobile.location l where 
    (g.City = l.city) and 
    (g.Country = l.country) limit 1 
    ) as latitude, 
    (select l.longitude from mobile.location l where 
    (g.City = l.city) and 
    (g.Country = l.country) limit 1 
    ) as longitude 
    from mobile.Geography g 
    WHERE (g.Advertiser ID = $mid) 
GROUP BY g.City; 
相關問題