I have to update 50000 stock levels from a csv file. I am running 2 SQL queries. One to obtain the productID from an EAN barcode and the other to update the stock. When iterating through a small batch of 1000 this completes in approx 1 second. When trying to run the full update (which I thought would run for approx 50 seconds) it runs for minutes until the server times out.
Is there any way I can optimise the sql or code?
The following 2 functions perform the update.
function _getIdFromEAN($ean){
global $conn;
global $db;
//Get product id from EAN
$sql = "SELECT entity_id FROM ".$db['pref']."catalog_product_entity_varchar WHERE attribute_id = 147 AND value = ". $ean;
if ($result = $conn->query($sql)):
if ($result->num_rows):
$row = $result->fetch_assoc();
return $row['entity_id'];
endif;
endif;
return false;
}
function _updateStocks($data){
global $conn;
global $db;
$ean = $data[0];
$newStockQty = $data[1] <= MIN_STOCK_LEVEL ? 0 : $data[1];
$isInStock = $newStockQty > 0 ? 1 : 0;
$productId = _getIdFromEAN($ean);
if ($productId):
$sql = "UPDATE ".$db['pref']."cataloginventory_stock_item csi,
".$db['pref']."cataloginventory_stock_status css
SET csi.is_in_stock = ".$isInStock.", csi.qty = ".$newStockQty."
WHERE csi.product_id = ".$productId ." AND csi.product_id = css.product_id";
if ($result = $conn->query($sql)):
return true;
endif;
endif;
return false;
}