不止一次地迭代mysqli无缓冲的查询结果

时间:2013-08-07 18:28:01

标签: php mysql unbuffered-queries

问题:

我有一个返回大结果集的查询。它太大了,无法引入PHP。我得到致命的内存最大错误,不能增加内存限制。 Unbuffered Queries

我需要多次迭代数组,但mysqli_data_seek不能用于无缓冲的查询。 mysqli_result::data_seek

//I have a buffered result set
$bresult = $mysql->query("SELECT * FROM Small_Table");

//And a very large unbuffered result set
$uresult = $mysqli->query("SELECT * FROM Big_Table", MYSQLI_USE_RESULT);

//The join to combine them takes too long and is too large
//The result set returned by the unbuffered query is too large itself to store in PHP
//There are too many rows in $bresult to re-execute the query or even a subset of it for each one

foreach($bresult as &$row) {
    //My solution was to search $uresult foreach row in $bresult to get the values I need
    $row['X'] = searchResult($uresult, $row['Key']);
    //PROBLEM: After the first search, $uresult is at its and and cannot be reset with mysqli_result::data_seek
}

function searchResult($uresult, $val)
    while($row = $uresult->fetch_assoc()){
        if($row['X'] == $val) {
            return $row['X'];
        }
    }
}

如果您有其他符合这些要求的解决方案,我会接受它:   - 不尝试在单个查询中加入结果(花费太长时间)   - 不对另一个查询中的每个结果运行任何查询(查询太多,耗时太长,系统速度变慢)

如果您需要更多信息,请发表评论。

谢谢。

1 个答案:

答案 0 :(得分:0)

如果您正在尝试处理大数据集,您是否考虑使用像Hadoop这样的中介?你可以设置一个小的hadoop集群,进行处理,然后让你的php代码向hadoop输出请求处理过的数据。