Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/267.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_Loops_Memory Management_Out Of Memory - Fatal编程技术网

用php处理大型结果集

用php处理大型结果集,php,loops,memory-management,out-of-memory,Php,Loops,Memory Management,Out Of Memory,我的场景是这样的,我从mysql表中获取了一个巨大的数据集 $data = $somearray; //say the number of records in this array is 200000 iam循环此数据,处理某些功能并将此数据写入excel文件 $my_file = 'somefile.csv'; $handle = fopen($my_file, 'w') or die('Cannot open file: ' . $my_file); file for($

我的场景是这样的,我从mysql表中获取了一个巨大的数据集

$data = $somearray; //say the number of records in this array is 200000
iam循环此数据,处理某些功能并将此数据写入excel文件

 $my_file = 'somefile.csv';
 $handle = fopen($my_file, 'w') or die('Cannot open file:  ' . $my_file); file
     for($i=0;$i<count($data);$i++){
         //do something with the data
         self::someOtherFunctionalities($data[$i]); //just some function    
         fwrite($handle, $data[$i]['index']); //here iam writing this data to a file
      }
 fclose($handle);

我不关心它所花费的时间。即使它需要几个小时。所以我确实设置了时间限制0。如果你不担心时间,一次取1000行,只需将行附加到文件的末尾,例如,制作一个临时文件,在工作完成时移动和/或重命名

First select count(*) from table
  then for($i = 0; i < number of row; i = i + 1000){
  result = SELECT * FROM table LIMIT i,1000; # Retrieve rows 6-15
  append to file = result
}
move and rename the file

这是很好的元代码,但是这个过程应该可以运行

您可以在MySQL查询中使用LIMIT吗

LIMIT子句可用于约束SELECT语句返回的行数。LIMIT接受一个或两个数值参数,这两个参数都必须是非负整数常量,除非使用准备好的语句

对于两个参数,第一个参数指定要返回的第一行的偏移量,第二个参数指定要返回的最大行数。初始行的偏移量为0而不是1:

从tbl限制5,10中选择*;检索第6-15行


您的工作是线性的,不需要加载所有数据。如果将此文件发送到httpClient,请使用“不临时文件”

<?php
$mysqli  = new mysqli("localhost", "my_user", "my_password", "world");
$uresult = $mysqli->query("SELECT Name FROM City", MYSQLI_USE_RESULT);
$my_file = 'somefile.csv'; // php://stdout
$handle = fopen($my_file, 'w') or die('Cannot open file:  ' . $my_file); file

if ($uresult) {
   while ($row = $uresult->fetch_assoc()) {
   // $row=$data[i]
      self::someOtherFunctionalities($row); //just some function    
     fwrite($handle, $row['index']); //here iam writing this data to a file
   }
}
$uresult->close();
?>

你不能分批读取MySQL数据或逐行处理它,而不是将整个集合保存到一个变量中吗?我也尝试过分批处理…但它仍然挂起…有没有什么方法可以释放每个循环末尾的内存释放内存只要将变量设为null,垃圾收集器应该处理内存。是否需要将所有数据提取到内存中?不能一次提取一行吗?是否使用PDO提取数据?查询需要按顺序进行,否则不能保证它将选择下一批。它还假设数据没有改变。请随意使用编辑按钮来改进我的建议,正如我所说的,它只是一个关于如何做的列表表单,而不是一个工作示例。
<?php
$mysqli  = new mysqli("localhost", "my_user", "my_password", "world");
$uresult = $mysqli->query("SELECT Name FROM City", MYSQLI_USE_RESULT);
$my_file = 'somefile.csv'; // php://stdout
$handle = fopen($my_file, 'w') or die('Cannot open file:  ' . $my_file); file

if ($uresult) {
   while ($row = $uresult->fetch_assoc()) {
   // $row=$data[i]
      self::someOtherFunctionalities($row); //just some function    
     fwrite($handle, $row['index']); //here iam writing this data to a file
   }
}
$uresult->close();
?>