2
I am building an import application that takes the lines of a txt file and updates it into two tables, works normally, however the files usually have more than 5 thousand lines, and I am not being able to update all tables with my current method. How could I optimize this code?
foreach($file as $line):
$row = explode(';', $line);
$prodcode = $row[0];
$prodcurrentinv = $row[3];
$prodprice = number_format(intval($row[5]), 2, '.', '') . '00';
$prodretailprice = $prodprice;
$query = $pdo->prepare('UPDATE isc_products SET prodcurrentinv = :prodcurrentinv, prodretailprice = :prodretailprice, prodprice = :prodprice WHERE prodcode = :prodcode LIMIT 1');
$query->bindParam(':prodcurrentinv', $prodcurrentinv, PDO::PARAM_STR, 11);
$query->bindParam(':prodretailprice', $prodretailprice, PDO::PARAM_STR, 11);
$query->bindParam(':prodprice', $prodprice, PDO::PARAM_STR, 11);
$query->bindParam(':prodcode', $prodcode, PDO::PARAM_STR, 12);
$query->execute();
$query = $pdo->prepare('UPDATE isc_product_variation_combinations SET vcstock = :vcstock WHERE vcsku = :vcsku LIMIT 1');
$query->bindParam(':vcstock', $prodcurrentinv, PDO::PARAM_STR, 11);
$query->bindParam(':vcsku', $prodcode, PDO::PARAM_STR, 12);
$query->execute();
endforeach;
You’re not getting why? What’s the mistake?
– uaiHebert
No error, The problem is the performance of the updates, as they are more than 5 thousand lines, so the code is running for about 2 minutes until it stops and ends up not doing the update of everything.
– Rafael Alexandre
There is no possibility to use ETL tools for this operation?
– Tiago César Oliveira
No, I think I would need to merge these two Updates, and use a better method to go through each line of the file.
– Rafael Alexandre
Why recreate the Pro-red statement in each loop? The purpose of Prepared statements is precisely to accelerate the interpretation process by creating it only once.
– Havenard