我有几千条记录(存储在MYSQL表的表中),我需要“批量处理”。所有记录都包含一个大型JSON。在某些情况下,JSON超过1MB(是的,我的数据库远远超过1GB)。
我有一个函数可以抓取记录,解码JSON,更改一些数据,将PHP数组重新编码回JSON,然后将其保存回数据库。很简单。 FWIW,这是在CakePHP应用程序的上下文中。
给定一个ID数组,我试图做这样的事情(非常简单的模拟代码):
foreach ($ids as $id) {
$this->Model->id = $id;
$data = $this->Model->read();
$newData = processData($data);
$this->Model->save($newData);
}
问题是,很快PHP就会耗尽内存。当运行这样的foreach时,几乎就像PHP从一个记录移动到下一个记录,而不释放前面操作所需的内存。
无论如何都要以这样的方式运行循环,即在继续循环的下一次迭代之前释放内存,这样我才能真正处理大量的数据?
编辑:添加更多代码。此函数接受我的JSON,将其转换为PHP数组,进行一些操作(即根据另一个数组中的内容重新配置数据),并替换原始数组中的值。 JSON有很多层深,因此极长的foreach循环。
function processData($theData) {
$toConvert = json_decode($theData['Program']['data'], $assoc = true);
foreach($toConvert['cycles'] as $cycle => $val) {
foreach($toConvert['cycles'][$cycle]['days'] as $day => $val) {
foreach($toConvert['cycles'][$cycle]['days'][$day]['sections'] as $section => $val) {
foreach($toConvert['cycles'][$cycle]['days'][$day]['sections'] as $section => $val) {
foreach($toConvert['cycles'][$cycle]['days'][$day]['sections'][$section]['exercises'] as $exercise => $val) {
if (isset($toConvert['cycles'][$cycle]['days'][$day]['sections'][$section]['exercises'][$exercise]['selectedFolder'])) {
$folderName = $toConvert['cycles'][$cycle]['days'][$day]['sections'][$section]['exercises'][$exercise]['selectedFolder']['folderName'];
if ( isset($newFolderList['Folders'][$folderName]) ) {
$toConvert['cycles'][$cycle]['days'][$day]['sections'][$section]['exercises'][$exercise]['selectedFolder'] = $newFolderList['Folders'][$folderName]['id'];
}
}
if (isset($toConvert['cycles'][$cycle]['days'][$day]['sections'][$section]['exercises'][$exercise]['selectedFile'])) {
$fileName = basename($toConvert['cycles'][$cycle]['days'][$day]['sections'][$section]['exercises'][$exercise]['selectedFile']['fileURL']);
if ( isset($newFolderList['Exercises'][$fileName]) ) {
$toConvert['cycles'][$cycle]['days'][$day]['sections'][$section]['exercises'][$exercise]['selectedFile'] = $newFolderList['Exercises'][$fileName]['id'];
}
}
}
}
}
}
}
return $toConvert;
}
Model-> read()实际上只是告诉Cake从db中提取记录,并将其返回到数组中。幕后发生了大量的事情,更有知识的人必须解释这一点。
答案 0 :(得分:2)
我要做的第一步是确保所有内容都通过引用传递。
例如,
foreach ($ids as $id) {
processData($data);
}
function processData(&$d){}