Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.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
mysql查询后释放内存_Mysql_Codeigniter_Memory - Fatal编程技术网

mysql查询后释放内存

mysql查询后释放内存,mysql,codeigniter,memory,Mysql,Codeigniter,Memory,我使用CodeIgniter从远程服务器导入大型表。我一次将查询分为1000行 这将是一个在后台运行的cron脚本 $userDB是本地DB对象,$remoteDB是远程DB对象 private function fetch_remote_db_table($remoteDB, $remoteTable, $localTable = FALSE){ $success = FALSE; $returnObj = (object) array(); //Use remote

我使用CodeIgniter从远程服务器导入大型表。我一次将查询分为1000行

这将是一个在后台运行的cron脚本

$userDB是本地DB对象,$remoteDB是远程DB对象

private function fetch_remote_db_table($remoteDB, $remoteTable, $localTable = FALSE){
    $success = FALSE;
    $returnObj = (object) array();

    //Use remote table name if local name not set.
    $localTable = ($localTable === FALSE) ? $remoteTable : $localTable;

    //Set the execution time and memory limit high, since this might take some work for the script
    ini_set('max_execution_time', 0);
    ini_set('memory_limit', '16000M');

    //Start by truncating the local table, which will be overwritten
    $this->userDB->truncate($localTable);

    //Get the remote table. This takes some time. Split up in chunks of 1000 rows
    $continue = TRUE;
    $counter = 0;
    while($continue){
        $limit = 1000;
        $offset = $counter*$limit;

        //Don't include offset in query if it's 0; CI will add it and break the query.
        if($offset == 0){
            $remoteDB->limit($limit);
        } else {
            $remoteDB->limit($limit, $offset);
        }

        $query = $remoteDB->get($remoteTable);
        $result = $query->result_array();

        if($query->num_rows() > 0){
            //Insert the remote data into the local table.
            if(!$this->userDB->insert_batch($localTable, $result)){$success = FALSE;}
        } else {$continue = FALSE;}
        $counter ++;
    }
    $this->output->enable_profiler(TRUE);
    var_dump(get_defined_vars());

    return $success;
}

我的问题是,每次迭代,结果都会留在内存中。我如何在每次迭代后从内存中清除结果?理想情况下,我希望保留查询中的元数据,但只删除所有行数据。

好的,经验教训。对于可能遇到相同问题的人:

还可以保存CI插入查询并占用内存

在我的设置中,我有: $remoteDB是我用来检索数据的远程连接 和 $userDB,它是到我的本地数据库的连接,我在那里插入了数据。这个有

 $userDB->save_queries = TRUE;
占用了我的记忆,没有看到任何变量被设置


在循环之前将其设置为FALSE之后,我可以在不耗尽内存的情况下处理任意多的数据。我可以保留查询元数据。

您可以使用$query->free_result();方法。我尝试了此方法,但可能使用不正确。我想它会在while循环的最后,对吗?这不会释放内存供我使用:在application/config/database.php中,您可以禁用以$db['default']['save_querys']=false收集sql查询;(如果现在是真的)是的,我相信我正确地使用了free_result()。我没有“保存查询”选项,但“缓存打开”设置为FALSE。您可以添加以下内容:$db['default']['save\u querys']=FALSE;或设置$this->db->save_querys=false;详细信息:
$this->db->save\u querys=false在控制器或模型中很方便。