【问题标题】:Magento 2 script wait due to CREATE TEMPORARY TABLE由于 CREATE TEMPORARY TABLE 而导致 Magento 2 脚本等待
【发布时间】:2019-11-17 08:27:31
【问题描述】:

我正在运行自定义产品导入脚本(导入具有额外属性的可配置简单产品、类别照片等)。

在使用($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 配置有关。 任何想法都非常受欢迎

【问题讨论】:

标签: php mysql mariadb magento2


【解决方案1】:

我认为新产品保存让您等待,因为它会根据您分配给该产品的类别在 url_rewrite 中为该产品创建一个条目

【讨论】:

  • 好吧,那是真的,但是如何避免创建临时表呢?
  • 创建一个 tmp 表对于在 URL 重写中进行条目是必要的
  • 那你觉得这个延迟正常吗?
  • 是的,这取决于您为产品分配了多少类别
  • 尝试在只有 PHP、Mysql 5.6 的小型“空白 vm”中运行项目,并且 mysql 日志中没有任何临时表。延迟从 70 秒下降到 14 秒,所以数据库肯定有问题(生产在 cpanel 中运行)
猜你喜欢
  • 1970-01-01
  • 2018-09-02
  • 1970-01-01
  • 1970-01-01
  • 2014-12-05
  • 1970-01-01
  • 2021-02-05
  • 2016-09-12
  • 2022-12-20
相关资源
最近更新 更多