我正在运行自定义产品导入脚本(导入具有额外属性、类别照片等的可配置的简单产品)。
在使用($categories 包含类别 ID 数组)将类别分配给新创建的产品时,我在脚本执行时面临奇怪的“等待”
$categoryLinkRepository->assignProductToCategories($product->getSku(), $categories);
经过调查,我发现当脚本在以下函数中执行第二个for循环时,延迟开始
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;
}
奇怪的是,同样的功能只对新创建的产品有延迟,当我为现有产品运行它时它运行良好。
服务器上的 Mysql 进程显示重复查询等待 66 秒
Sending data CREATE TEMPORARY TABLE `tmp_select_tUFtPSHTf3LaHnv19OnapfygPIfXxdCU` (PRIMARY KEY(`url_rewrite_id`),
我只在生产服务器(运行 MariaDB 10.3.16)上遇到这个问题,而不是在我的本地开发 VM(Mysql 5.7)上遇到这个问题
我认为问题与服务器上的 mysql 配置有关。 任何想法都非常受欢迎
最佳答案
我认为新产品节省会让您等待,因为它会根据您分配给该产品的类别在该产品的 url_rewrite 中创建一个条目
关于php - Magento 2 脚本等待创建临时表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56924479/