Website in PHP + MYSQL. Third-party site provides XML upload. I process ready unloading on ODA. conditions and pass on your website in the database. You need to implement a batch implementation to reduce the load and data write time. Parsing goes through xpath. In foreach there is a SELECT, conditions with continue and 1 INSERT. How to break into packages - I'll never know.

foreach ($xmlTree->xpath('/doct//page/product') as $tree) { $prid = (array) $tree->product; $prid = $prid[0]; $pgid = (array) $tree->page; $pgid = $pgid[0]; //Проверяем, есть ли товар с таким id $ge = db::getInstance()->db()->query('SELECT id FROM goods_from_site WHERE product_id=?d', $prid); if(!empty($ge)){ continue; } db::getInstance()->db()->query('INSERT INTO `goods_from_site` (`product_id`, `rid2`) VALUES ("'.$prid.'","'.$pgid.'")'); } 
  • one
    first you need to see what a few select, whether they are really needed. as long as there is a select for each line, you can forget about batch processing. And by the way, the main thing in this matter is the prepared (once before the cycle) requests, so that only execute (and possibly bind) inside the cycle is Mike
  • I check if there is a product with such an ID, if not, then I add it. If there is, then turn to another. foreach ($xmlTree->xpath('/doct//page/product') as $tree) { $prid = (array) $tree->product; $prid = $prid[0]; $pgid = (array) $tree->page; $pgid = $pgid[0]; //Проверяем, есть ли товар с таким id $ge = db::getInstance()->db()->query('SELECT id FROM goods_from_site WHERE product_id=?d', $prid); if(!empty($ge)){ continue; } db::getInstance()->db()->query('INSERT INTO foreach ($xmlTree->xpath('/doct//page/product') as $tree) { $prid = (array) $tree->product; $prid = $prid[0]; $pgid = (array) $tree->page; $pgid = $pgid[0]; //Проверяем, есть ли товар с таким id $ge = db::getInstance()->db()->query('SELECT id FROM goods_from_site WHERE product_id=?d', $prid); if(!empty($ge)){ continue; } db::getInstance()->db()->query('INSERT INTO goods_from_site` ( product_id , rid2 ) VALUES ("'. $ prid. '", "'. $ pgid. '") "); } - LEgXr
  • one
    If the product_id column has a unique index (or it is a primary key, or a unique restriction), then you can use insert IGNORE into ... and you don’t need a check, it will only insert new rows. Plus, you need to go to the prepared expressions. before the loop, do $q=prepare('insert .... values(?,?) , and in the loop execute $q->execute( array($prid,$pgid ); (for PDO, in the case of mysqli you will have to do bind inside the loop. Up to 50% of the insert operation time in the database is taken by compiling a new query from the text - Mike
  • And there are no other options besides PDO? - LEgXr
  • similar to mysqli, only there it is impossible to transfer parameters to execute, it is necessary to do bind, but this should not affect speed, just a bit more complicated code - Mike

0