在PHP中有没有比array_diff更快的方法

时间:2017-01-18 02:27:25

标签: php mysql

我有一组来自MySQL的数字,范围是1000 0000(8位)到9 999 999 999(10位)。它应该是连续的,但是缺少数字。我需要知道哪些号码丢失了。

范围很大。起初我打算用PHP来做这件事:

//MySqli Select Query
$results = $mysqli->query("SELECT `OCLC Number` FROM `MARC Records by Number`");

$n_array = array();
while($row = $results->fetch_assoc()) {
    $n_array[] = $row["OCLC Number"];
}

d($n_array);
foreach($n_array as $k => $val) {
    print $val . " ";
}

/* 8 digits */
$counter = 10000000;
$master_array = array();

/* 10 digits */
while ($counter <= 9999999999 ) {
    $master_array[] = $counter;
    $counter++;
    d($master_array);
}
d($master_array);    

$missing_numbers_ar = array_diff ($master_array, $n_array);
d($missing_numbers_ar);

d()是一个类似于var_dump()的自定义函数。

然而,我刚刚意识到这需要花费大量时间才能完成。在15分钟标记处,$ master_array仅填充了4000个数字。

我怎样才能更快地完成这项工作?仅限MySQL或MySQL和PHP解决方案都受到欢迎。如果最佳解决方案取决于缺少的数量,请告诉我如何解决。 TQ

2 个答案:

答案 0 :(得分:1)

您的d()可能是导致速度缓慢的原因,请将其删除,并对代码进行细微更改

while($row = $results->fetch_assoc()) {
    $n_array[$row["OCLC Number"]] = 1;
}

$missing_numbers_ar = [];

while ($counter++ <= 9999999999 ) {
    if (empty($n_array[$counter])) {
        $missing_numbers_ar[] = $counter;        
    }
}

答案 1 :(得分:0)

如果以下情况仍然缓慢,我会感到惊讶。我也注意到它与@Hieu Vo的答案相似。

// Make sure the data is returned in order by adding
// an `ORDER BY ...` clause.
$results = $mysqli->query("SELECT `OCLC Number` 
            FROM `MARC Records by Number` 
            ORDER BY `OCLC Number`");

$n_array = array();
while($row = $results->fetch_assoc()) {
    // Add the "OCLC Number" as a key to the array.
    $n_array[$row["OCLC Number"]] = $row["OCLC Number"];
}

// assume the first array key is in fact correct
$i = key($n_array);
// get the last key, also assume it is not missing.
end($n_array);
$max = key($n_array);
// reset the array (should not be needed)
reset($n_array);
do {
    if (! $n_array[$i]) {
        echo 'Missing key:['.$i.']<br />';
        // flush the data to the page as you go.
        flush();
    }
} while(++$i <= $max);