thinkphp大量数据插入,内存溢出

浏览:5551 发布日期:2014/11/05 分类:求助交流 关键字: 数据库 内存
 public function sysncOther($table,$link){
        //echo 'table:'.sysncOther."。";
        $t1=microtime(true);
                
           $index=0;
           $each_num=1000;//每次同步1000条数据
           $from_table=$table;//xx库的表名
        $to_table=$table;//本系统的表名
        $rcount=odbc_exec($link,"select count(*) from  $from_table");
        while(odbc_fetch_row($rcount)){
            $count = odbc_result($rcount,1);
        }
        //echo $count.'<br/>';
        //删除原数据
        $m=M($to_table);
        $m->execute("delete from $to_table");
        $list=M("information_schema.columns")->field("COLUMN_NAME")->where(array('TABLE_NAME'=>$to_table))->select();
        
        while($index<$count){
            $m=M($to_table);
            $ms1=array();
            $ms1[]=memory_get_usage();
            $temp=$index+$each_num;
            $tt=odbc_exec($link,"select * from (select a.*,rownum rn from $from_table a where rownum<=$temp) where rn>$index");
            $index += $each_num;
            $datas=array();
            while(odbc_fetch_row($tt)){
                $data=array();
                foreach ($list as $v){
                    $data[$v['COLUMN_NAME']]=odbc_result($tt,strtolower($v['COLUMN_NAME']));
                }
                
                $datas[]=$data;
            }
            $m->addAll($datas);//如果注释这条代码,每次执行后内存基本不变
            unset($datas);
            unset($tt);
            unset($data);
            $t2=microtime(true);
            $ms1[]=memory_get_usage();
            print_r($ms1);
            echo str_repeat(' ', 100);
            echo '<br/>';
            ob_flush();
            flush();
            
            //echo "<br/>take:".round($t2-$t1,3);
        }
   }
//最近要做一个数据同步,发现同步一会就内存溢出,一个表大概几十w条数据,每同步1000条数据,占用内存多了1.6m。如果注释了这条代码            $m->addAll($datas);,则内存的占用基本不变。是thinkphp的问题还是是我的代码有问题,求解
最佳答案
评论( 相关
后面还有条评论,点击查看>>