Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 优化查找属于两个不同表的两个横向/纵向点之间的最小距离所需的时间_Php_Mysql - Fatal编程技术网

Php 优化查找属于两个不同表的两个横向/纵向点之间的最小距离所需的时间

Php 优化查找属于两个不同表的两个横向/纵向点之间的最小距离所需的时间,php,mysql,Php,Mysql,我目前有两个表,每个表大约有40k行。每个存储了lat/long,我需要从“表2”中找出lat/long,对于“表1”的每个lat/long,它至少与“表1”的lat/long保持距离 出于这个目的,我使用了以下查询,但是当我的查询遍历整个40k*40k行时,需要花费很多时间,因此我可能会寻找一些同样可以进行的优化 $select_Table1 = 'Select id, latn, longe from table1'; $table1Data = mysqli_query($

我目前有两个表,每个表大约有40k行。每个存储了lat/long,我需要从“表2”中找出lat/long,对于“表1”的每个lat/long,它至少与“表1”的lat/long保持距离

出于这个目的,我使用了以下查询,但是当我的查询遍历整个40k*40k行时,需要花费很多时间,因此我可能会寻找一些同样可以进行的优化

    $select_Table1 = 'Select id, latn, longe from table1';
    $table1Data = mysqli_query($connection,$select_Table1) or die("error :".mysqli_error($connecttion));
    $radius = 6371;//Mean radius of earth
    $deg2Rad = (pi()/180);

    while($table1_rows = mysqli_fetch_row($table1Data))
    {
        $table1Id = $table1_rows[0];
        $table1Lat = $table1_rows[1];
        $table1Long = $table1_rows[2];
        $a = "Select $radius * acos(sin($deg2Rad*($table1Lat)) * sin($deg2Rad*(table2.lat)) +  cos($deg2Rad*($table1Lat)) * cos($deg2Rad*(table2.lat)) * cos($deg2Rad*($table1Long-table2.long))) as Distance from table2 order by Distance limit 1";
        $result = mysqli_query($connection, $a) or die("Error :".mysqli_error($connection));
        while($rows = mysqli_fetch_row($result))
        {
            //Some calculations here
        }
    }

此外,表格是动态的,它们经常更改,因此无法进行预计算

查看空间数据的扩展:。请参阅连接。在循环内执行查询是个糟糕的主意。请查看空间数据的扩展:。请参阅连接。在循环内执行查询是一个糟糕的想法。