用户
 找回密码
 入住 CI 中国社区
搜索
查看: 1239|回复: 1
收起左侧

关于大数据循环插入表中的效率问题

[复制链接]
发表于 2015-3-11 15:05:57 | 显示全部楼层 |阅读模式

  1. 写了个将MSSQL中的表数据转存到MYSQL中对应的表里的方法
  2. MSSQL中用到的表是:PE_Supply(20多万条数据)
  3. Mysql中用到的表有wx_sell_31,wx_sell_data_31,wx_member,wx_company
  4. 由于PE_Supply表里涉及到了用户信息,和公司信息,所以将PE_Supply表里的内容直接插入到wx_sell_31中之前,先要验证里面的用户和公司信息是否存在于我Mysql数据库中,否则不能插入
  5. 现在的问题就是,也许是这个验证问题导致了效率降低,循环100条数据插入到我mysql中非常的慢,想看看大家有没有可以优化的地方提升下我的效率
复制代码
PHP复制代码
 
function importgongying(){
        //查询当前mysql表中最新一条记录
        $lastarticle = $this->db->query('select * from wx_sell_31 order by itemid desc limit 1')->row_array();
        if(empty($lastarticle)) $startid = 1;//如果没有记录指定起始id=1
        else $startid = $lastarticle['itemid'];
        //开启MSSQL连接
        $db = $this->conmssql();
        //根据起始ID,查询PE_Supply表中100条数据
        $rs = $db->query("select * from (select row_number() over (order by supplyid) as row_num,* from PE_Supply where supplyid>$startid and channelid=999 and classid=134 and username is not null) t where row_num between 1 and 10");
 
        $rs->setFetchMode(PDO::FETCH_ASSOC);
        //将查询结果返回到$article数组中
        $article = $rs->fetchAll();
        //循环article数组,将pe_supply中的数据转存到mysql中
        if(!empty($article)){
            $count = 0;
            $r = null;
            $rstr = 1;
            foreach ($article as $val){
                if($val['USERNAME']==null){
                    $r[] = array('aid'=>0,'articleid'=>$val['SUPPLYID']);
                    continue;
                }
                //查询用户信息
                $user = $this->db->query("select truename from wx_member where username='{$val['USERNAME']}'")->row_array();
                if(empty($user)){
                    $user['truename'] = '';
//                     $r[] = array('aid'=>0,'articleid'=>$val['SUPPLYID']);continue;
                }
                //查询公司信息
                $com = $this->db->query("select groupid,company,vip,telephone,address,mail from wx_company where username='{$val['USERNAME']}'")->row_array();
                if(empty($com)){
                    $rs = $db->query("delete from PE_Supply where username='{$val['USERNAME']}'");
                    $r[] = array('aid'=>0,'articleid'=>$val['SUPPLYID']);
                    $rstr=0;
                    break;
//                     $this->importgongying();
                }
                $catid = 16951;
                $introduce = mb_substr(strip_tags($val['SUPPLYINTRO']),0,100);
                $linkurl = 'show.php?itemid='.$val['SUPPLYID'];
                $keyword = '';
                if(trim($val['SUPPLYPICURL'])!=null){
                    $pics = explode('|',$val['SUPPLYPICURL']);
                    if($this->checkUrl($pics[0])){
                        $thumb = $pics[0];
                    }else{
                        $thumb = 'http://www.chinahvacr.com/Supply/Uploadphotos/'.$pics[0];
                    }
                }
                else $thumb = '';
                $edittime = strtotime($val['UPDATETIME']);
                //将用户信息和公司信息以及article信息存放到数组中
                $arr = array(
                        'itemid'=>$val['SUPPLYID'],
                        'catid'=>$catid,
                        'mycatid'=>0,
                        'typeid'=>0,
                        'areaid'=>1,
                        'level'=>0,
                        'title'=>$val['SUPPLYTITLE'],
                        'introduce'=>$introduce,
                        'keyword'=>'',
                        'hits'=>$val['HITS'],
                        'thumb'=>$thumb,
                        'username'=>$val['USERNAME'],
                        'groupid'=>$com['groupid'],
                        'company'=>$com['company'],
                        'vip'=>$com['vip'],
                        'truename'=>$user['truename'],
                        'telephone'=>$com['telephone'],
                        'address'=>$com['address'],
                        'email'=>$com['mail'],
                        'editor'=>$val['USERNAME'],
                        'addtime'=>$edittime,
                        'editdate'=>date('Y-m-d',$edittime),
                        'edittime'=>$edittime,
                        'adddate'=>date('Y-m-d',$edittime),
                        'status'=>3,
                        'linkurl'=>$linkurl
                );
                //插入数据到mysql中
                $this->db->insert('wx_sell_31',$arr);
//                                 echo $this->db->last_query();exit;
                $aid = $this->db->insert_id();
                $content = str_replace('[InstallDir_ChannelDir]{$UploadDir}','http://www.chinahvacr.com/Supply/Uploadphotos',$val['SUPPLYINTRO']);
                $arr1 = array(
                        'itemid'=>$aid,
                        'content'=>$content
                );
                //将内容插入到mysql中
                $this->db->insert('wx_sell_data_31',$arr1);
                unset($arr);
                unset($arr2);
                $r[] = array('aid'=>$aid,'articleid'=>$val['SUPPLYID']);
                if($aid>0){
                    $count++;
                }
            }
            $result = array('count'=>$count,'article'=>$r,'rstr'=>$rstr);
            echo json_encode($result);
            unset($result);
            unset($r);
            unset($article);
            unset($rs);
        }else{
            echo 'no data';
        }
 
复制代码

发表于 2015-3-12 11:46:53 | 显示全部楼层
java效率高。。。

本版积分规则