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

在PHP中有没有比array_diff更快的方法,php,mysql,Php,Mysql,我有一组MySQL中的数字,范围为1000 0000 8位到9 999 999 10位。它应该是连续的,但缺少数字。我需要知道丢失了哪些号码 范围很大。起初,我打算使用PHP来实现这一点: //MySqli Select Query $results = $mysqli->query("SELECT `OCLC Number` FROM `MARC Records by Number`"); $n_array = array(); while($row = $results->fe

我有一组MySQL中的数字,范围为1000 0000 8位到9 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_数组只填充了4000个数字

我怎样才能更快地做到这一点?只支持MySQL或MySQL和PHP解决方案。如果最佳解决方案取决于缺少多少个数字,请告诉我是如何做到的。Tq.

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

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


如果下面的内容仍然很慢,我会感到惊讶。我还注意到它与@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);

dupe:你的表中似乎不太可能有100亿条记录。为什么不将数据库中的所有ID转储到一个平面文件中,对其进行排序,然后在文本文件中逐行迭代,看看有什么差距?这通常运行得很快。可能是
// 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);