5

我有几千条记录(存储在 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 从数据库中提取一条记录,并将其返回到一个数组中。幕后发生了很多事情,需要更有知识的人来解释一下。

4

1 回答 1

2

我要做的第一步是确保所有内容都通过引用传递。

例如,

foreach ($ids as $id) {
processData($data);
}

function processData(&$d){}

http://php.net/manual/en/language.references.pass.php

于 2013-04-19T02:11:50.100 回答