【发布时间】:2013-08-09 06:42:08
【问题描述】:
问题:
我有一个返回大型结果集的查询。它太大而不能带入 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'];
}
}
}
如果您有其他满足这些要求的解决方案,我会接受: - 不尝试在单个查询中加入结果(花费太长时间) - 不对另一个查询中的每个结果运行任何查询(查询太多、耗时太长、降低系统速度)
如果您需要更多信息,请发表评论。
谢谢。
【问题讨论】:
-
我可以在用户
mysqli::real_query之后多次调用mysqli::use_result吗? -
SELECT * FROM gigantic_table是任何应用程序的死亡之吻。您将需要使用LIMIT和OFFSET来获取更小的数据块。还可以尝试仅检索您绝对需要的列。 -
@tadman 我实际上并没有做那个查询。这只是示例代码。我只查询所需的数据。
-
很高兴知道。不过,您至少需要
LIMIT。如果您真的遇到麻烦,您可以随时使用SELECT ... INTO OUTFILE将数据转储到纯文本文件中,并以您想要的任何方式进行解析。
标签: php mysql unbuffered-queries