同步订单数据,数据量大解决方案

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_35669659/article/details/77081891
public function index()
    {
        $current_time=date('Y-m-d H:i:s',time());//当前时间
        $this->db_gbt= $this->load->database('gbt', TRUE);
        $sql="select * from tm_sync_time where sync_type='tmnew'";     
        $query=$this->db_gbt->query($sql);
        $arrtime= $query->result_array();
        $sync_time=$arrtime[0]['sync_time'];
        //print_r($sync_time);
        $start_time=date("Y-m-d H:i:s", strtotime("{$sync_time} -10 minute"));//开始时间
        echo $start_time;
        $this->db_default= $this->load->database('default', TRUE);
        //$sql="select COUNT(*) from  jdp_tb_trade where jdp_modified<'2016-03-01 19:00:00' and  jdp_created >= '2016-03-01 00:00:00' ";
        $sql="select COUNT(*) from  jdp_tb_trade where  jdp_modified between '{$start_time}' and  '{$current_time}'   and  jdp_created >= '2016-03-01 00:00:00' ";
        $query=$this->db_default->query($sql);
        $ordernum=$query->row_array();
        $totalnum=$ordernum['COUNT(*)'];
        $num=ceil($totalnum/1000);
        echo $totalnum."---".$num."<br/>";
        for($i=0;$i<$num;$i++){
                $start=$i*1000;
                $sql="select * from  jdp_tb_trade where jdp_modified between '{$start_time}' and  '{$current_time}'  and  jdp_created >= '2016-03-01 00:00:00' limit {$start},1000";
                //$sql="select * from  jdp_tb_trade where jdp_modified<'2016-03-01 19:00:00' and  jdp_created >= '2016-03-01 00:00:00' limit {$start},1000";
                $query=$this->db_default->query($sql);
                $orderarr=$query->result_array();
                foreach($orderarr as $k=>$v){
                $data=$v;
                $where=array('tid'=>"{$v['tid']}");
                $sql="SELECT * FROM jdp_tb_trade WHERE tid ='{$v['tid']}'";
                //$sql="SELECT * FROM jdp_tb_trade WHERE tid ='20202'";
                $query=$this->db_gbt->query($sql);
                $tidarr=$query->row_array();
                //print_r($tidarr);
                if($tidarr!=array()){
                $this->db_gbt->update('jdp_tb_trade', $data, $where);
                echo $v['tid']."更新";
                }else{
                $this->db_gbt->insert('jdp_tb_trade', $data);
                    echo $v['tid']."插入";
                }       
                //echo $this->db_gbt->last_query();
                //exit;
                }           
        }
        $data=array('sync_time'=>"{$current_time}");
        $where=array('sync_type'=>"tmnew");
        $this->db_gbt->update('tm_sync_time', $data, $where);
    }
展开阅读全文

没有更多推荐了,返回首页