php & mysql 插入大量数据三种方式的性能差距

数量级5000条


<?php

namespace appadmincommand;

use thinkconsoleCommand;
use thinkconsoleInput;
use thinkconsoleOutput;
use appadminmodelModel;
use appcommonlogicXls;
// 将信息导入到表里面去
class putQxxXls extends Command
{
    protected function configure()
    {
        $this->setName('putQxxXls')->setDescription('Here is the remark ');
    }

    protected function execute(Input $input, Output $output)
    {
        $path = APP_PATH . 'admin/lib/qxxdata.xls';
        $model = new Model();
        $data = Xls::excel2data($path);
        $fileds = ["qy_name", "second_name", "qy_address"];


        $time = Xls::getMs();

        // float(1493)
        // 使用原生sql语句实现批量插入
        // array_walk($data, function (&$value, $key){
        //     array_walk($value, function(&$value1, $key1){
        //         $value1 = "'" . $value1 . "'";
        //     });
        //     $value = "(" .implode(',', $value). ")";
        // });
        // $data = implode(',', $data);
      
        // $sql1 = "INSERT INTO `crm_brandtest_qcc`(`qy_name`,`second_name`,`qy_address`)VALUES " .  $data;
        // $model->execute($sql1);

        // float(133122)
        // 使用tp的saveAll实现批量插入
        // array_walk($data, function (&$value, $key) use ($fileds){
        //     for ($i=0; $i < count($fileds); $i++) { 
        //         $value[$fileds[$i]] = $value[$i];
        //         unset($value[$i]);
        //     }
        // });
        // $model->saveAll($data);

        // float(133514)
        // 在循环里面save实现批量插入
        // foreach($data as $k => $v){
        //     for ($i=0; $i < count($fileds); $i++) { 
        //         $v[$fileds[$i]] = $v[$i];
        //         unset($v[$i]);
        //     }
        //     $model->insert($v);
        // }
        var_dump(Xls::getMs() - $time);

    }
}
原文地址:https://www.cnblogs.com/cl94/p/13964416.html