如何解决用雄辩的
我正在使用Maatwebsite从excel文件中导入数据,在我的模型中创建新行之前,我检查寄存器是否已经存在以避免重复。但这需要太长时间。
在我的ProductImport.PHP
中:
public function model(array $row)
{
$exists = Product::
where('product_description',$row["product_description"])
->where('product_code',$row["product_code"])
->first();
if($exists ){
return null;
}
++$this->rows;
// Autoincrement id
return new Product([
"product_description" => $row["art_descripcion"],"product_code" => $row["cui"],"id_user" => $this->id_user,...
]);
}
public function chunkSize(): int
{
return 1000;
}
如您所见,我还使用了chunkSize,因为每个excel有5000行。
问题:
product_description
的大小在800到900个字符(varchar [1000])之间变化,并且每次where()
中的每次迭代都使查询(foreach
)非常慢。 >
是否有更好的方法来处理此问题?也许使用updateOrCreate
而不是先搜索然后创建?因为我认为这是相同的方法。
所以主要问题是如何更快比较800-900大小的字符串?由于此搜索需要花费大量时间才能执行:
$exists = Product::
where('product_description',$row["product_code"])
->first();
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。