[英]Laravel Insert/Update/Delete
I currently have this code below, however when adding around 2000 rows this runs too slow due to being in an foreach loop. 我目前在下面有此代码,但是当添加约2000行时,由于处于foreach循环中,因此运行速度太慢。
foreach($tables as $key => $table) {
$class_name = explode("\\", get_class($table[0]));
$class_name = end($class_name);
$moved = 'moved_' . $class_name;
${$moved} = [];
foreach($table[0]->where('website_id', $website->id)->get() as $value) {
$value->website_id = $live_website_id;
$value->setAppends([]);
$table[0]::on('live')->updateOrCreate([
'id' => $value->id,
'website_id' => $value->website_id
], $value->toArray());
${$moved}[] = $value->id;
}
// Remove deleted rows
if ($table[1]) {
$table[0]::on('live')->where([ 'website_id' => $live_website_id ])
->whereNotIn('id', ${$moved})
->delete();
}
}
What is happening is basically users will add/update/delete data in a development server, then when they hit a button this data needs to be pushed into the live table, retaining the ID's as auto incremental id's on the live won't work due to look-up tables and multiple users launching data live at the same time. 发生的事情基本上是用户将在开发服务器中添加/更新/删除数据,然后当他们按下按钮时,该数据需要被推送到活动表中,保留该ID,因为实时的自动增量ID将无法使用查找表和多个用户同时实时启动数据。
What is the best way to do this? 做这个的最好方式是什么? Should I simply remove all the data in that table (there is a unique identifier for chunks of data) and then just insert? 我是否应该简单地删除该表中的所有数据(数据块有唯一的标识符)然后插入?
I think can do: 我认为可以做到:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.