php to mysql batch insert

1. Insert data one by one into insert

echo 'start: 'date('Y-m-d H-i-s').PHP_EOL;
$count = 0;
for ($i = 1;$i <= 100000 ;$i++) {
    
    
    $add_data = [
        'id'       => $i,
        'username' => 'user'.$i,
        'sex'      => mt_rand(1,2),
        'age'      => mt_rand(10,60),
    ];

    $res = Db::connect('db_test')->table('tf_user')->insert($add_data);
    if ($res) {
    
    
        $count ++ ;
    }
}
echo 'over: 'date('Y-m-d H-i-s').PHP_EOL;
echo "成功添加".$count."个数据。".PHP_EOL;

10W pieces of data, it took nearly 85 minutes. The above code requires less memory. The execution time is relatively long.

2.insertAll insert multiple items at once in batch

$all_data = [];
echo 'start: 'date('Y-m-d H-i-s').PHP_EOL;
$begin = 100001;
$end   = 200000;
$count = 0;
$total_count = 0;
for ($i = $begin;$i <= $end ;$i++) {
    
    
    $add_data = [
        'id'       => $i,
        'username' => 'user'.$i,
        'sex'      => mt_rand(1,2),
        'age'      => mt_rand(10,60),
    ];
    $all_data[] = $add_data;
    if (count($all_data) == 5000) {
    
      //一次插入5000条
        $count = Db::connect('db_test')->table('tf_user')->insertAll($all_data);
        $total_count += $count;
        $all_data = []; // 重置
    }
}
echo 'over: 'date('Y-m-d H-i-s').PHP_EOL;
echo '完成添加'.$total_count."条";

It took less than 6 minutes, and the second program obviously took a lot faster, but it would require more memory.

The sql generated by the insertAll method:

INSERT INTO `tf_user` (`id` , `username` , `sex` , `age`) VALUES ( 100001,'user100001',2,47 ) , ( 100002,'user100002',2,26 ) , ( 100003,'user100003',1,10 ) , ( 100004,'user100004',2,10 ) , ( 100005,'user100005',2,50 )

Reprinted: Comparison of two programs inserting 10W data

Guess you like

Origin blog.csdn.net/qq_39004843/article/details/105920974