问题描述
我正在使用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 (将#修改为@)