I’m receiving from an external API a list with 3.000 values (car part codes).
Now I’m trying to get from my database all entries that contains one of these codes.
$q = Product::query();
$q->where('codigo', '');
foreach($keys as $v){
$q->orWhere('codfabrica', $v); // varchar 191
$q->orWhere('gtin', $v); // varchar 191
$q->orWhere('codoriginal', $v); // varchar 191
$q->orWhere('codreferencia', $v); // varchar 191
$q->orWhere('aplicacao', 'LIKE', '%'.$v.'%'); // medium text
}
$found = $q->get();
This is taking 10 minutes but it works.
How can I speed up or optimize this query?
2
Answers
Try chunking the list into smaller subsets and perform multiple queries.
To speed up this I guess you can make an array of the values you are getting from API and check with the whereIn query.