搜索数千条记录(属性)并返回数据


Searching through thousands of records (properties) and returning the data

所以我已经实现了一个谷歌地图,使用javascript绘制一个多边形。我使用getBounds来获取latLongs。然后我调用一个数据库(postcodes),它包含英国每个邮政编码的每个lat长。

这个查询看起来像:

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

返回正确的数据。

然后我有第二个数据库address_list,它包含英国的每个地址(目前它只包含Kettering区域,所以12,000行)。我在第一个查询上做一个while循环,然后在每个循环上运行这个查询:

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

然后我将邮政编码的latlong添加到一个变量:

$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服务…

有3种源代码可以使代码变慢:mysql, php, js.

我要做的第一件事是在像toad这样的工具中运行sql(连接版本),或者执行输出原始结果的php文件。您可以添加console.time/console。timeEnd在js中,而microtime在php中。

另一个"快速和肮脏"的检查是过去的"your_server"/php/properties.php?边界= YourBounds,然后检查结果。它会给你一些提示。

如果你确定它是sql,尝试索引digital_hub.address_list。邮政编码,postcode.postcodeGoogle。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数据。(并检查您对长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');

});