I'm running a custom products import script ( importing configurable, simple products with extra attributes, categories photos etc ).
I am facing a strange "wait" at script execution when assigning categories to a newly created product using ( $categories contains an array of category ids )
$categoryLinkRepository->assignProductToCategories($product->getSku(), $categories);
After investigation , i've found that the delay starts when the script executes the 2nd for loop in the following function
public function assignProductToCategories($productSku, array $categoryIds)
{
$product = $this->getProductRepository()->get($productSku);
$assignedCategories = $this->getProductResource()->getCategoryIds($product);
foreach (array_diff($assignedCategories, $categoryIds) as $categoryId) {
$this->getCategoryLinkRepository()->deleteByIds($categoryId, $productSku);
}
foreach (array_diff($categoryIds, $assignedCategories) as $categoryId) {
/** @var \Magento\Catalog\Api\Data\CategoryProductLinkInterface $categoryProductLink */
$categoryProductLink = $this->productLinkFactory->create();
$categoryProductLink->setSku($productSku);
$categoryProductLink->setCategoryId($categoryId);
$categoryProductLink->setPosition(0);
$this->getCategoryLinkRepository()->save($categoryProductLink);
}
$productCategoryIndexer = $this->getIndexerRegistry()->get(Indexer\Product\Category::INDEXER_ID);
if (!$productCategoryIndexer->isScheduled()) {
$productCategoryIndexer->reindexRow($product->getId());
}
return true;
}
The strange thing is that the same function faces a delay only for newly created products, when i run it for an existing products its runs fine.
Mysql Processes at the server shows a repeating query waiting for 66secs
Sending data CREATE TEMPORARY TABLE `tmp_select_tUFtPSHTf3LaHnv19OnapfygPIfXxdCU` (PRIMARY KEY(`url_rewrite_id`),
I am only facing this issue at the production server ( Running MariaDB 10.3.16 ) and not on my local development VM ( Mysql 5.7 )
I think the issue is related with mysql configuration at the server. Any ideas are more than welcome