gabrielbuzzi
gabrielbuzzi

Reputation: 353

Problems with slow queries

Problem

I have a magento store, this store run an script that sync intranet products and store, this script run each 1 hour, my server administrator block the site access because this script is getting slow queries.

Question

My script just read a CSV file and for each row check if the SKU already registered, if already update weight, stock and price, have a better way to do this?

this is my script.

$produto = Mage::getModel('catalog/product');
$productId = $produto->getIdBySku($sku);
$produto->load($productId);

if ($produto->getId()) {
    $stock = $produto->getStockData();
    $stock['qty'] = $quantidade;
    $stock['is_in_stock'] = $this->initStock($quantidade);
    $stock['manage_stock'] = 1;
    $stock['use_config_manage_stock'] = 1;
    $produto->setData('price', $preco);
    if ($altura > 0)
        $produto->setData('volume_altura', $altura);
    if ($comprimento > 0)
        $produto->setData('volume_comprimento', $comprimento);
    if ($largura > 0)
        $produto->setData('volume_largura', $largura);
    $produto->setData('weight', $this->initWeight($peso));
    $produto->setStockData($stock);

    $produto->save();
} else {
    $produto = Mage::getModel('catalog/product');
    $produto->setTypeId('simple');
    $produto->setAttributeSetId(4);
    $produto->setData('tax_class_id', 0);
    $produto->setVisibility(1);
    $produto->setStatus(1);
    $produto->setData('sku', $sku);
    $produto->setData('color', $cor);
    $produto->setData('name', utf8_encode($descricao));
    $produto->setData('marca', $this->initAttribute(148, $marca));
    $produto->setData('codigo_barra', $codBarra);
    $produto->setData('price', $preco);
    if ($altura > 0)
        $produto->setData('volume_altura', $altura);
    if ($comprimento > 0)
        $produto->setData('volume_comprimento', $comprimento);
    if ($largura > 0)
        $produto->setData('volume_largura', $largura);
    $produto->setData('cost', $custo);
    $stock['qty'] = $quantidade;
    $stock['is_in_stock'] = $this->initStock($quantidade);
    $stock['manage_stock'] = 1;
    $stock['use_config_manage_stock'] = 1;
    $produto->setStockData($stock);
    $produto->setData('weight', $this->initWeight($peso));

    $produto->save();
}

Upvotes: 0

Views: 37

Answers (1)

Alberto
Alberto

Reputation: 31

Is there any way to reduce the number of items in the CSV file? I mean, if you send all items every time and for each one of them you make a query, the number of queries you make and the time that will take the script to finish will be too high.

I recommend you to only sync with the store the new items or the items with updates values, it will decrease the number of rows in the CSV and the number of queries too, also, if you include in your CSV file a field with a flag Updated/New or something like this, you can avoid the first query to check if the product exists in the case of the "New" flag is found.

I hope it helps.

Upvotes: 1

Related Questions