laravel 实现 100w 大量数据插入数据库# --- 2

2023-02-23  本文已影响0人  一位先生_
 set_time_limit(0);

 try {
    //上传文件位置,这里默认是在storage中,如有修改请对应替换
    $file = storage_path('/app/public/' . $input['file']);
    $domain = [];
    foreach($this->readTxt($file) as $key=>$line) {
        $domain[$key] = $line;
    }
    //数组分块处理
    $chunck = collect($domain);
    $chunks = $chunck->chunk(1000);
    $chunks->all();
    foreach ($chunks as $key=>$val){
        $arr = [];
        foreach ($val as $k =>$value){
            $arr[$k]['domain'] = $value;
            $arr[$k]['created_at'] = date('Y-m-d H:i:s');
        }
        DB::table('domain')->insert($arr);
   }
 }catch (\Exception $e) {
    return $this->response()->error($e->getMessage());
 }

参考文章:https://learnku.com/articles/62505

上一篇下一篇

猜你喜欢

热点阅读