php pdo测试批量insert into 和单条insert into对比

             // 单条插入
   for ($i = 0; $i < 1000; $i++) {
            $name             = rand(1000, 9999);
            $email            = rand(1000, 9999);
            $message          = rand(1000, 9999);
            $insertdata       = array('name' => $name, 'email' => $email, 'message' => $message);
           $this->commoninsertinfo($insertdata,'aa');
        }
          

时间1分钟。。。太慢了

 

然后看看批量插入

      $insertdatalist = array();
        for ($i = 0; $i < 1000; $i++) {
            $name             = rand(1000, 9999);
            $email            = rand(1000, 9999);
            $message          = rand(1000, 9999);
            $insertdata       = array('name' => $name, 'email' => $email, 'message' => $message);
            $insertdatalist[] = $insertdata;
        }
              $this->insertss($insertdatalist, 'aa');

0.01秒。。快1000倍 或者更多

 

测试过插入100W数据 1秒多。。。配置文件

修改 my.ini 加上 max_allowed_packet =67108864
67108864=64M

设置的足够大

 

ps:如果限麻烦 可以直接用事务         时间10秒钟 不过事务最好不要超过500  可以每次运行封装到函数里 这样会更快       这个方法适用于上面2种 都会更快

  public function ssss()
    {
        \Db::beginTransactions();
        for ($i = 0; $i < 500; $i++) {
            $name       = rand(1000, 9999);
            $email      = rand(1000, 9999);
            $message    = rand(1000, 9999);
            $insertdata = array('name' => $name, 'email' => $email, 'message' => $message);
            $this->commoninsertinfo($insertdata, 'aa');
        }

        \Db::commits();
    }
    public function shiwutest1()
    {
        set_time_limit(0);
        ini_set("memory_limit", "512M");
        $begintime = time();
        $this->ssss();
        $this->ssss();
        $endtime = time();
        echo $endtime - $begintime;

    }

 

posted @ 2020-11-25 20:56  newmiracle宇宙  阅读(344)  评论(0编辑  收藏  举报