Eloquent batch update of multiple records (update when present, insert when not present)
It is not a batch assignment of multiple fields to a record.
Similar BULK INSERT:
DB::table('users')->insert(array( array('email' => 'aaa@example.com', 'name' => 'zhangsan', 'age'=> 0), array('email' => 'bbb@example.com', 'name' => 'wangwu', 'age'=> 0), ...));
There are no similar statements:
DB::table('users')->update( array( array('email' => 'aaa@example.com', 'name' => 'zhangsan', 'age'=> 20), array('email' => 'bbb@example.com', 'name' => 'wangwu', 'age'=> 25), array('email' => 'ccc@example.com', 'name' => 'chenliu', 'age'=> 50), ...) , 'email' );
The functions implemented are:
1. When the query conditions exist, batch update the original data;
例:email='aaa@example.com'时, 'age'修改为 20,email='bbb@example.com'时, 'age'修改为 25,...
2. When the query condition does not exist, BULK insert the data.
例:email='ccc@example.com'时, 'age'修改为 50,...
My Code is:
public function updateOrCreate (Request $request) { $insert_array = []; $datas = $request->all(); foreach ($datas as $key=> $data) { $user = User::where('email', $data['email'])->first(); if (!$user) { $insert_array[] = $data; // 更新原数据 } else { $user->email = $data['email']; $user->age = $data['age']; $user->save(); } } // 批量插入数据 User::insert($insert_array);}
The above code, when the update data for more than thousand pen, there is a performance problem!
For advice, is there a better way to solve it?
Please give me more guidance.
Reply content:
Eloquent batch update of multiple records (update when present, insert when not present)
It is not a batch assignment of multiple fields to a record.
Similar BULK INSERT:
DB::table('users')->insert(array( array('email' => 'aaa@example.com', 'name' => 'zhangsan', 'age'=> 0), array('email' => 'bbb@example.com', 'name' => 'wangwu', 'age'=> 0), ...));
There are no similar statements:
DB::table('users')->update( array( array('email' => 'aaa@example.com', 'name' => 'zhangsan', 'age'=> 20), array('email' => 'bbb@example.com', 'name' => 'wangwu', 'age'=> 25), array('email' => 'ccc@example.com', 'name' => 'chenliu', 'age'=> 50), ...) , 'email' );
The functions implemented are:
1. When the query conditions exist, batch update the original data;
例:email='aaa@example.com'时, 'age'修改为 20,email='bbb@example.com'时, 'age'修改为 25,...
2. When the query condition does not exist, BULK insert the data.
例:email='ccc@example.com'时, 'age'修改为 50,...
My Code is:
public function updateOrCreate (Request $request) { $insert_array = []; $datas = $request->all(); foreach ($datas as $key=> $data) { $user = User::where('email', $data['email'])->first(); if (!$user) { $insert_array[] = $data; // 更新原数据 } else { $user->email = $data['email']; $user->age = $data['age']; $user->save(); } } // 批量插入数据 User::insert($insert_array);}
The above code, when the update data for more than thousand pen, there is a performance problem!
For advice, is there a better way to solve it?
Please give me more guidance.
1. Change the query to DB
action
2. foreach
do not have query operation inside
3. All emails can be assembled with a query statement query, which is the current record by the server.