2011-08-18 89 views
0

所以我實現了一個谷歌地圖,使用JavaScript來繪製一個多邊形。我使用getBounds來獲取latLongs。然後我打電話給一個數據庫(postcodes),其中包含英國每個郵政編碼的每個長度。通過數千條記錄(屬性)搜索並返回數據

這個查詢看起來像:

SELECT * FROM `postcode`.`postcodeGoogle` WHERE (`lat` BETWEEN '$lat1' AND '$lat2') 
    AND (`long` BETWEEN '$long1' AND '$long2') 

這很好,返回正確的數據。

然後我有第二個數據庫,address_list,其中包含英國的每個地址(目前它只包含凱特林區,所以12000行)。我做的第一查詢while循環,然後在每次循環運行此查詢:

SELECT * FROM `digital_hub`.`address_list` WHERE `postcode`='".$row['postcode']."' 

然後我郵政編碼的經緯度添加到一個變量:

$resultE = $resultE."(".$row['lat'].", ".$row['long'].") || ";

這則得到呼應在循環的最後。

此頁面通過jQuery稱爲:

$.get("php/properties.php?bounds="+bounds, function(data){ 
     var theLatLongs = data.split(" || "); 
     totalcount = 0; 

     for(x in theLatLongs){ 
      var theLatLong = ""; 
      var latLong = theLatLongs[x].substr(1); 
      var latLong = latLong.slice(0, -1); 
      var theLatLong = latLong.split(", "); 
      var thelat = theLatLong[0]; 
      var thelong = theLatLong[1]; 

       totalcount = totalcount+1; 

     } 
    $('#totalcount').html('<h6><span>'+totalcount+'</span> Households found</h6>Filtered by location'); 
}); 

這一切工作正常(我認爲)。這非常慢。我知道Facebook有更好的資源,但創建廣告過濾器的速度非常快。我也將實施更多的過濾器。我嘗試了加入,但時間差異似乎並不大。有時它甚至不會返回結果,並崩潰我的mysql服務...

回答

1

有3個源可以減慢代碼:mysql,php,js。

我要做的第一件事就是運行sql(連接版本),在像蟾蜍這樣的工具中,或者做一個輸出原始結果的php文件。您可以將console.time/console.timeEnd添加到js中,並在php中添加mictrotime。

另一個「快速和髒」的檢查是通過「your_server」/php/properties.php?bounds=YourBounds, 並檢查結果。它會給你一些指示。

如果您確定它是sql,請嘗試索引digital_hub.address_list.postcodes,postcode.postcodeGoogle.lat和postcode.postcodeGoogle.long。

然後,在您的php「raw」腳本(或您的sql工具)中,嘗試使用較少的列, 或甚至用「select count(*)」調用查詢。如果「select count(*)」的速度更快,那意味着返回值會降低系統速度。這是典型的空(非空)日期字段的例子。

但他們的關鍵是簡單的,時間不同的部分來隔離瓶頸的過程。

console.time('load'); 
$.get("php/properties.php?bounds="+bounds, function(data){ 
     console.timeEnd('load'); 
     console.time('parse'); 
     var theLatLongs = data.split(" || "); 
     totalcount = 0; 

     for(x in theLatLong){ 
      var theLatLong = ""; 
      var latLong = theLatLongs[x].substr(1); 
      var latLong = latLong.slice(0, -1); 
      var theLatLong = latLong.split(", "); 
      var thelat = theLatLong[0]; 
      var thelong = theLatLong[1]; 

     totalcount = totalcount+1; 
     } 
console.timeEnd('parse'); 
    console.time('display');  
    $('#totalcount').html('<h6><span>'+totalcount+'</span> Households found</h6>Filtered by location'); 
    console.timeEnd('display');   
}); 

在旁註中,您可能會考慮json數據。(和審查你的thelatlong JS變種的使用,但我猜你正在調試) 在PHP中:

while ($row = msqlqresult) { 
    $resultArray[] = $row; 
} 

$resultJson = json_encode($resultArray); 

在JS:

console.time('load'); 
$.getJSON("php/properties.php", {bounds: bounds}, function(data){ 
     console.timeEnd('load'); 
     console.time('parse'); 
     totalcount = 0; 

     for(var x in data){ 
      var thelat = theLatLong[x].lat; 
      var thelong = theLatLong[x].long; 

      totalcount = totalcount+1; 
     } 
    console.timeEnd('parse'); 
    console.time('display');  
    $('#totalcount').html('<h6><span>'+totalcount+'</span> Households found</h6>Filtered by location'); 
    console.timeEnd('display'); 

});

+0

謝謝!我會放棄這一點! – rickyduck

+1

注意,我更新了一下代碼,使用getJSON。 – roselan

+0

甜美的男人,也使它更快,而不是使用之間,使用'<=' and '> =' – rickyduck