溪溪不是溪 发表于 2013-6-1 17:32:50

如何让我的SQL更高效,插入成千上万条数据

for($v=1;$v<=$b;$v++)      {
      $request->setCurPage($v);
                $yihaodian = new yihaodianCommon();
          $result = $yihaodian->sendByPost($request);
      $result = json_decode($result,true);
      $b = ceil($result['response']['totalCount']/100);
      if($b)
      {
             foreach ($result['response']['orderList']['order'] as $val)
             {
                           $date['orderId'] = $val['orderId'];
                           $date['orderCode'] = $val['orderCode'];
                           $date['orderAmount'] = $val['orderAmount'];
                           $date['productAmount'] = $val['productAmount'];
                           $date['orderStatus'] = $val['orderStatus'];
                           $date['orderCreateTime'] = $val['orderCreateTime'];
                           $date['orderNeedInvoice'] = $val['orderNeedInvoice'];
                           $date['orderDeliveryFee'] = $val['orderDeliveryFee'];
                           $date['updateTime'] = $val['updateTime'];
                           $test[] = $date;

                           $sql = $this->orderlist_model->order_get($date['orderCode']);
                           if($sql)
                           {
                                  echo "订单号"."<font color=\"red\">".$date['orderCode']."</font>"."已存在"."<br>";
                           }
                           else
                           {
                                  $this->db->insert('orderget',$date);
                                  echo "订单号"."<font color=\"red\">".$date['orderCode']."</font>"."下载成功"."<br>";
                           }
                          
             }
      }
      }
这是一个通过开放API批量获取某个电商平台订单列表的程序
程序每次只能读取一页,每页100条订单信息,将这100条订单信息循环插入数据库,插入之前判断数据库中是否已存在该条记录。
循环插入一页(也就是100条之后),再循环获取下一页信息再排量插入数据库。代码见上图!
问:这样每插入一次数据库要访问两次数据库,如果要插入上万条信息就要访问上万次!如何能在保证成功插入订单记录的前提下优化此程序呢?
如果我一次插入100条要怎么判断这100条记录中是否在数据库中已经存在。

smartweb 发表于 2013-6-4 20:05:27

内存很重要。我试过PHP操作60万条记录,内存相差太大,速度相差很大的,现在130万条了,2G内存还一般般。
页: [1]
查看完整版本: 如何让我的SQL更高效,插入成千上万条数据