0

所以我遇到了这个问题,我已经从供应商自动将产品导入到 akeneo,在尝试升级代码的性能时我破坏了一些东西,现在我遇到了子类别的问题,无法从产品中访问它们,一些供应商运行良好,一些错误。现在整整一周我都在尝试连接现在发生的事情我遇到了这个类别表在此处输入图像描述

我不确定这些列的lft rgt含义,这不是我第一次看到它们,但从未真正弄清楚它们的作用。只是坚持也许这可以帮助我调试东西

4

2 回答 2

0

对于由于某种原因嵌套集已完成的问题完全相同的人。

在做任何事情之前,请备份您的数据库!

在另一篇文章的帮助下:如何使用 SQL 修复数据库中损坏的 MPTT 树(嵌套集)?

更改了 sql 以使用 Akeneo 类别:

将此过程添加到您的数据库中:

DROP PROCEDURE IF EXISTS tree_recover;

DELIMITER //

CREATE PROCEDURE tree_recover ()
MODIFIES SQL DATA
BEGIN

    DECLARE currentId, currentParentId  CHAR(36);
    DECLARE currentLeft                 INT;
    DECLARE startId                     INT DEFAULT 1;

    # Determines the max size for MEMORY tables.
    SET max_heap_table_size = 1024 * 1024 * 512;

    START TRANSACTION;

    # Temporary MEMORY table to do all the heavy lifting in,
    # otherwise performance is simply abysmal.
    CREATE TABLE `tmp_tree` (
        `id`        int(36) NOT NULL,
        `parent_id` int(36),
        `lft`       int(11) unsigned,
        `rgt`      int(11)  unsigned,
        PRIMARY KEY      (`id`),
        INDEX USING HASH (`parent_id`),
        INDEX USING HASH (`lft`),
        INDEX USING HASH (`rgt`)
    ) ENGINE = MEMORY
    SELECT `id`,
           `parent_id`,
           `lft`,
           `rgt`
    FROM   `pim_catalog_category`;

# Leveling the playing field.
UPDATE  `tmp_tree`
SET     `lft`  = 0,
        `rgt` = 0;
# Establishing starting numbers for all root elements.
WHILE EXISTS (SELECT * FROM `tmp_tree` WHERE `parent_id` IS NULL AND `lft` = 0 AND `rgt` = 0 LIMIT 1) DO


        UPDATE `tmp_tree`
        SET    `lft`  = startId,
               `rgt` = startId + 1
        WHERE  `parent_id` IS NULL
          AND  `lft`       = 0
          AND  `rgt`      = 0
        LIMIT  1;

        SET startId = startId + 2;

    END WHILE;

    # Switching the indexes for the lft/rgt columns to B-Trees to speed up the next section, which uses range queries.
    DROP INDEX `lft`  ON `tmp_tree`;
    DROP INDEX `rgt` ON `tmp_tree`;
    CREATE INDEX `lft`  USING BTREE ON `tmp_tree` (`lft`);
    CREATE INDEX `rgt` USING BTREE ON `tmp_tree` (`rgt`);

    # Numbering all child elements
    WHILE EXISTS (SELECT * FROM `tmp_tree` WHERE `lft` = 0 LIMIT 1) DO

        # Picking an unprocessed element which has a processed parent.
        SELECT     `tmp_tree`.`id`
          INTO     currentId
        FROM       `tmp_tree`
        INNER JOIN `tmp_tree` AS `parents`
                ON `tmp_tree`.`parent_id` = `parents`.`id`
        WHERE      `tmp_tree`.`lft` = 0
          AND      `parents`.`lft`  <> 0
        LIMIT      1;

        # Finding the element's parent.
        SELECT  `parent_id`
          INTO  currentParentId
        FROM    `tmp_tree`
        WHERE   `id` = currentId;

        # Finding the parent's lft value.
        SELECT  `lft`
          INTO  currentLeft
        FROM    `tmp_tree`
        WHERE   `id` = currentParentId;

        # Shifting all elements to the right of the current element 2 to the right.
        UPDATE `tmp_tree`
        SET    `rgt` = `rgt` + 2
        WHERE  `rgt` > currentLeft;

        UPDATE `tmp_tree`
        SET    `lft` = `lft` + 2
        WHERE  `lft` > currentLeft;

        # Setting lft and rgt values for current element.
        UPDATE `tmp_tree`
        SET    `lft`  = currentLeft + 1,
               `rgt` = currentLeft + 2
        WHERE  `id`   = currentId;

    END WHILE;

    # Writing calculated values back to physical table.
    UPDATE `pim_catalog_category`, `tmp_tree`
    SET    `pim_catalog_category`.`lft`  = `tmp_tree`.`lft`,
           `pim_catalog_category`.`rgt` = `tmp_tree`.`rgt`
    WHERE  `pim_catalog_category`.`id`   = `tmp_tree`.`id`;

    COMMIT;

    DROP TABLE `tmp_tree`;

END//

DELIMITER ;

并调用它:

CALL tree_recover;

在您的 Akeneo 数据库中

此命令可能需要几分钟到几天的时间,具体取决于您拥有的类别数量

请注意:通过执行此命令,新的 lft 和 rgt 计算值会在该过程完成后立即提交。

于 2022-01-12T06:03:48.913 回答
0

这是一个嵌套集模型,它是一种在关系数据库中表示嵌套集(也称为树或层次结构)的技术。

您可以在此处找到有关此模型的更多信息

于 2020-08-29T00:01:32.767 回答